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).

https://docs.python.org/3/library/multiprocessing.html?highlight=multiprocessing%20map#multiprocessing.pool.pool.map


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -