multithreading - Webscrape multithread python 3 -
i have been dong simple webscraping program learn how code , made work wanted see how make faster. wanted ask how implement multi-threading program? program open stock symbols file , searches price stock online.
here code
import urllib.request import urllib threading import thread symbolsfile = open("stocklist.txt") symbolslist = symbolsfile.read() thesymbolslist = symbolslist.split("\n") i=0 while i<len (thesymbolslist): theurl = "http://www.google.com/finance/getprices?q=" + thesymbolslist[i] + "&i=10&p=25m&f=c" thepage = urllib.request.urlopen(theurl) # read correct character encoding `content-type` request header charset_encoding = thepage.info().get_content_charset() # apply encoding thepage = thepage.read().decode(charset_encoding) print(thesymbolslist[i] + " price " + thepage.split()[len(thepage.split())-1]) i= i+1
if iterate function on list, recommend multiprocessing.pool.map(function, list)
.
Comments
Post a Comment