複数ページのWebサイトを解析したい。Pythonマルチプロセッシング - オンデマンドでワーカーを使用する
私はページ数を知りません。 これは元のコードです:
next_button=soup.find_all('a',{'class':"btn-page_nav right"})
while next_button:
link=next_button[0]['href']
resp=requests.get('webpage+link)
soup=BeautifulSoup(resp.content)
table=soup.find('table',{'class':'js-searchresults'})
body=table.find('tbody')
rows=body.find_all('tr')
function(rows)
next_button=soup.find_all('a',{'class':"btn-page_nav right"})
それは正常に動作し、function(rows)
は、各ページの一部を解析する機能です。
私がしたいのは、これらのページを解析するのにmultiprocessing
を使用することです。一度に3ページを処理できるように、私はpool
3人の労働者を使用することを考えましたが、それを実装する方法を理解できません。その後、
rows_list=[]
next_button=soup.find_all('a',{'class':"btn-page_nav right"})
while next_button:
link=next_button[0]['href']
resp=requests.get('webpage+link)
soup=BeautifulSoup(resp.content)
table=soup.find('table',{'class':'js-searchresults'})
body=table.find('tbody')
rows=body.find_all('tr')
rows_list.append(rows)
next_button=soup.find_all('a',{'class':"btn-page_nav right"})
すべてのページをループへのプログラムのための待ちと::
一つの解決策はこれです
pool=multiprocessing.Pool(processes=4)
pool.map(function,rows_list)
しかし、私は、これはあまりにも多くのパフォーマンスを向上させるとは思わない、私メインプロセスがページをループしてページを開くとすぐに、そのプロセスをワーカーに送ります。 どうすればいいですか?ダミー例:
pool=multiprocessing.Pool(processes=4)
next_button=soup.find_all('a',{'class':"btn-page_nav right"})
while next_button:
link=next_button[0]['href']
resp=requests.get('webpage+link)
soup=BeautifulSoup(resp.content)
table=soup.find('table',{'class':'js-searchresults'})
body=table.find('tbody')
rows=body.find_all('tr')
**pool.send_to_idle_worker(rows)**
next_button=soup.find_all('a',{'class':"btn-page_nav right"})