码迷,mamicode.com
首页 > 其他好文 > 详细

爬虫神器xpath的用法(三)

时间:2016-03-06 12:52:33      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

xpath的多线程爬虫

#encoding=utf-8
‘‘‘
pool = Pool(4) cpu的核数为4核
results = pool.map(爬取函数,网址列表)
‘‘‘
from multiprocessing.dummy import Pool as ThreadPool
import requests
import time

def getsource(url):
    html = requests.get(url)

urls = []

for i in range(1,21):
    newpage = http://tieba.baidu.com/p/3522395718?pn= + str(i)
    urls.append(newpage)

time1 = time.time()
for i in urls:
    print i
    getsource(i)
time2 = time.time()
print u单线程耗时: + str(time2-time1)

pool = ThreadPool(4)
time3 = time.time()
results = pool.map(getsource, urls)
pool.close()
pool.join()
time4 = time.time()
print u并行耗时: + str(time4-time3)

输出:

单线程耗时:12.0818030834
并行耗时:3.58480286598

爬虫神器xpath的用法(三)

标签:

原文地址:http://www.cnblogs.com/gide/p/5246809.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!