利用Python怎么样实现一个并发爬虫-创新互联
利用Python怎么样实现一个并发爬虫?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

一.顺序抓取
顺序抓取是最最常见的抓取方式,一般初学爬虫的朋友就是利用这种方式,下面是一个测试代码,顺序抓取8个url,我们可以来测试一下抓取完成需要多少时间:
HEADERS = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Accept-Encoding': 'gzip, deflate',}
URLS = ['/tupian/20230522/3248890.html
'/tupian/20230522/newest&
'http://blog.csdn.net/yueguanghaidao/article/details/24281751',
'/tupian/20230522/36987&
'http://blog.chinaunix.net/uid-9162199-id-4738168.html',
'http://www.tuicool.com/articles/u67Bz26',
'http://rfyiamcool.blog.51cto.com/1030776/1538367/',
'http://itindex.net/detail/26512-flask-tornado-gevent']
#url为随机获取的一批url
def func():
"""
顺序抓取
"""
import requests
import time
urls = URLS
headers = HEADERS
headers['user-agent'] = "Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537" \
".36+(KHTML,+like+Gecko)+Chrome/45.0.2454.101+Safari/537.36"
print(u'顺序抓取')
starttime= time.time()
for url in urls:
try:
r = requests.get(url, allow_redirects=False, timeout=2.0, headers=headers)
except:
pass
else:
print(r.status_code, r.url)
endtime=time.time()
print(endtime-starttime)
func() 网站名称:利用Python怎么样实现一个并发爬虫-创新互联
文章转载:http://www.scyingshan.cn/article/ddcejj.html


咨询
建站咨询
