コードを最適化する方法を探していたときに、スレッドと urllib3 について良いことを耳にしました。どうやら、人々はどの解決策が最善かについて意見が分かれているようです。
以下のスクリプトの問題は、実行時間です。とても遅いです!
ステップ 1 : このページを取得します http://www.cambridgeesol.org/institutions/results.php?region=Afghanistan&type=&BULATS=on
ステップ 2 : BeautifulSoup でページを解析します
ステップ 3:データを Excel ドキュメントに入れる
ステップ 4:リスト (大きなリスト) のすべての国に対して、何度も何度も繰り返します (URL の「アフガニスタン」を別の国に変更しているだけです)。
これが私のコードです:
ws = wb.add_sheet("BULATS_IA") #We add a new tab in the excel doc
x = 0 # We need x and y for pulling the data into the excel doc
y = 0
Countries_List = ['Afghanistan','Albania','Andorra','Argentina','Armenia','Australia','Austria','Azerbaijan','Bahrain','Bangladesh','Belgium','Belize','Bolivia','Bosnia and Herzegovina','Brazil','Brunei Darussalam','Bulgaria','Cameroon','Canada','Central African Republic','Chile','China','Colombia','Costa Rica','Croatia','Cuba','Cyprus','Czech Republic','Denmark','Dominican Republic','Ecuador','Egypt','Eritrea','Estonia','Ethiopia','Faroe Islands','Fiji','Finland','France','French Polynesia','Georgia','Germany','Gibraltar','Greece','Grenada','Hong Kong','Hungary','Iceland','India','Indonesia','Iran','Iraq','Ireland','Israel','Italy','Jamaica','Japan','Jordan','Kazakhstan','Kenya','Kuwait','Latvia','Lebanon','Libya','Liechtenstein','Lithuania','Luxembourg','Macau','Macedonia','Malaysia','Maldives','Malta','Mexico','Monaco','Montenegro','Morocco','Mozambique','Myanmar (Burma)','Nepal','Netherlands','New Caledonia','New Zealand','Nigeria','Norway','Oman','Pakistan','Palestine','Papua New Guinea','Paraguay','Peru','Philippines','Poland','Portugal','Qatar','Romania','Russia','Saudi Arabia','Serbia','Singapore','Slovakia','Slovenia','South Africa','South Korea','Spain','Sri Lanka','Sweden','Switzerland','Syria','Taiwan','Thailand','Trinadad and Tobago','Tunisia','Turkey','Ukraine','United Arab Emirates','United Kingdom','United States','Uruguay','Uzbekistan','Venezuela','Vietnam']
Longueur = len(Countries_List)
for Countries in Countries_List:
y = 0
htmlSource = urllib.urlopen("http://www.cambridgeesol.org/institutions/results.php?region=%s&type=&BULATS=on" % (Countries)).read() # I am opening the page with the name of the correspondant country in the url
s = soup(htmlSource)
tableGood = s.findAll('table')
try:
rows = tableGood[3].findAll('tr')
for tr in rows:
cols = tr.findAll('td')
y = 0
x = x + 1
for td in cols:
hum = td.text
ws.write(x,y,hum)
y = y + 1
wb.save("%s.xls" % name_excel)
except (IndexError):
pass
だから私はすべてが完璧ではないことを知っていますが、Python で新しいことを学ぶことを楽しみにしています! urllib2 はそれほど高速ではなく、BeautifulSoup. スープについては、私は本当にそれを改善することはできないと思いますが、urllib2 についてはそうしません。
編集 1: urllib2 では役に立たないマルチプロセッシング? 私の場合は面白いようです。この潜在的な解決策についてどう思いますか?!
# Make sure that the queue is thread-safe!!
def producer(self):
# Only need one producer, although you could have multiple
with fh = open('urllist.txt', 'r'):
for line in fh:
self.queue.enqueue(line.strip())
def consumer(self):
# Fire up N of these babies for some speed
while True:
url = self.queue.dequeue()
dh = urllib2.urlopen(url)
with fh = open('/dev/null', 'w'): # gotta put it somewhere
fh.write(dh.read())
EDIT 2: URLLIB3 誰か私にそれについてもっと教えてもらえますか?
複数のリクエスト (HTTPConnectionPool および HTTPSConnectionPool) に同じソケット接続を再利用します (オプションのクライアント側の証明書検証を使用)。 https://github.com/shazow/urllib3
異なるページに対して同じ Web サイトを 122 回要求している限り、同じソケット接続を再利用するのは面白いと思いますが、間違っていますか? もっと速くできないの?...
http = urllib3.PoolManager()
r = http.request('GET', 'http://www.bulats.org')
for Pages in Pages_List:
r = http.request('GET', 'http://www.bulats.org/agents/find-an-agent?field_continent_tid=All&field_country_tid=All&page=%s' % (Pages))
s = soup(r.data)