我想让我的实际爬虫多线程。
当我设置多线程时,该函数的几个实例将被启动。避免重复结果多线程Python
例:
如果我的功能我用print range(5)
,我将有1,1,2,2,3,3,4,4,5,5
如果我有2个线程。
如何在Multithread中得到结果1,2,3,4,5
?
我实际的代码是履带,你可以看到下:
import requests
from bs4 import BeautifulSoup
def trade_spider(max_pages):
page = 1
while page <= max_pages:
url = "http://stackoverflow.com/questions?page=" + str(page)
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
for link in soup.findAll('a', {'class': 'question-hyperlink'}):
href = link.get('href')
title = link.string
print(title)
get_single_item_data("http://stackoverflow.com/" + href)
page += 1
def get_single_item_data(item_url):
source_code = requests.get(item_url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
res = soup.find('span', {'class': 'vote-count-post '})
print("UpVote : " + res.string)
trade_spider(1)
我怎么能调用多线程trade_spider()
不重复的链接?
您是否尝试过使用[shared_ multiprocessing.Value'](https://docs.python.org/2/library/multiprocessing.html#sharing-state-between-processes)? –
还没有,我会尝试 – Pixel
@DavidCullen请你给我一个例子,我不明白共享多处理在文档中是如何工作的。谢谢 – Pixel