您的位置:首页 > 编程语言 > Python开发

Python 利用多线程进行爬虫(一)

2017-12-30 21:21 465 查看
这几天帮同学爬取拉勾网的招聘信息,真好学习一下多线程爬虫,之前做过几次爬虫都是单线程的。看到网上说python由于GIL的存在,多线程的效果不好。但是当处理IO比较密集的任务时,网络请求时间较长时,多线程可以充分利用cpu资源。

参考链接:
https://www.ibm.com/developerworks/cn/aix/library/au-threadingpython/ https://www.cnblogs.com/mr-zys/p/5059451.html
1、先了解一下线程和进程的区别,这方面资料很多。

创建 一个url队列和out队列,url队列用来存放需要爬取的网站链接,out队列用来存放每个url爬取到的数据。

queue = Queue.Queue()
out_queue = Queue.Queue()


2、构造header,设置伪登录

request_headers = {
'host': "www.lagou.com",
'connection': "keep-alive",
'cache-control': "no-cache",
'user-agent': "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36",
'accept': "application/json, text/javascript, */*; q=0.01",
'accept-language': "zh-CN,zh;q=0.9",
'cookie':"   "
}


3、使用多线程从url队列中获得url进行解析,并把得到的结果放入到结果队列中out_queue

def run(self):
while True:
#grabs url from queue
url = self.queue.get()
#grabs urls of hosts and prints first 1024 bytes of page
requset = urllib2.Request(url,headers=request_headers)
try:
response=urllib2.urlopen(requset)
allcon=response.read()
soup = BeautifulSoup(allcon,"html.parser",from_encoding="gb18030")
except urllib2.URLError, e:
print e.reason
res = self.get_result(soup)
out_queue.put(res)
#signals to queue job is done
self.queue.task_done()
其中,queue.get()从队列中得到一个item,并将其进行删除,queue.task_done()监督该任务是否处理完毕

4、将结果从结果队列out_queue()中取出并写入到文件中。

def run(self):
while True:
res = self.out_queue.get()
#             print res
#parse the chunk
f.write(res.encode('utf-8'))

#signals to queue job is done
self.out_queue.task_done()


5、填充队列,对队列执行join()操作

for i in range(1,4):
t = ThreadUrl(queue,out_queue)
t.setDaemon(True)
t.start()
type = 'Java'
for i in range(1,31):
url = 'https://www.lagou.com/zhaopin/'+str(type)+'/'+str(i)+'/?filterOption='+str(i)+'&city=北京'
queue.put(url)

for i in range(3):
t = WriteThread(out_queue)
t.setDaemon(True)
t.start()

queue.join()
out_queue.join()


结果:

高级java开发工程师	[北京]	1天前发布	20k-30k	经验5-10年 / 本科	拉勾网thttps://www.lagou.com/gongsi/147.html	企业服务,招聘 / D轮及以上
java开发工程师	[望京]	08:57发布	25k-35k	经验3-5年 / 本科	丽兹行thttps://www.lagou.com/gongsi/124195.html	企业服务,广告营销 / 不需要融资
Java开发	[望京]	1天前发布	25k-35k	经验3-5年 / 本科	美团点评thttps://www.lagou.com/gongsi/50702.html	移动互联网,O2O / D轮及以上
Java	[东直门]	1天前发布	15k-25k	经验3-5年 / 本科	集奥聚合(GEO)thttps://www.lagou.com/gongsi/13179.html	数据服务 / B轮
Java开发工程师	[西二旗]	2017-12-20	15k-25k	经验3-5年 / 本科	易点租thttps://www.lagou.com/gongsi/63582.html	电子商务 / B轮
Java工程师	[团结湖]	1天前发布	20k-35k	经验1-3年 / 本科	洋钱罐thttps://www.lagou.com/gongsi/110502.html	移动互联网,金融 / 不需要融资
Java	[中关村]	1天前发布	18k-30k	经验3-5年 / 本科	新东方在线thttps://www.lagou.com/gongsi/9933.html	移动互联网,教育 / 不需要融资
Java工程师	[来广营]	1天前发布	15k-25k	经验3-5年 / 本科	e代驾thttps://www.lagou.com/gongsi/51835.html	移动互联网,O2O / D轮及以上
JAVA	[望京]	1天前发布	15k-25k	经验3-5年 / 本科	普奥云信息thttps://www.lagou.com/gongsi/91666.html	数据服务 / A轮
Java开发工程师	[团结湖]	2天前发布	15k-25k	经验3-5年 / 本科	资产360thttps://www.lagou.com/gongsi/48622.html	移动互联网,O2O / B轮
Java开发工程师	[三元桥]	2天前发布	15k-25k	经验3-5年 / 本科	友信thttps://www.lagou.com/gongsi/6616.html	金融 / 不需要融资
java开发工程师	[西北旺]	1天前发布	18k-25k	经验3-5年 / 本科	跟谁学thttps://www.lagou.com/gongsi/35361.html	移动互联网,教育 / A轮
JAVA开发工程师	[北京大学]	1天前发布	15k-25k	经验3-5年 / 本科	AbleCloudthttps://www.lagou.com/gongsi/43655.html	硬件 / B轮
JAVA开发工程师	[八角]	2天前发布	15k-23k	经验3-5年 / 本科	畅游thttps://www.lagou.com/gongsi/280.html	移动互联网,游戏 / 上市公司
Java开发工程师	[朝阳门]	1天前发布	14k-20k	经验3-5年 / 本科	云帐房thttps://www.lagou.com/gongsi/140172.html	移动互联网 / B轮
Java高级开发工程师	[苏州街]	1天前发布	20k-40k	经验5-10年 / 本科	拉勾网thttps://www.lagou.com/gongsi/147.html	企业服务,招聘 / D轮及以上
高级Java研发工程师	[望京]	02:17发布	30k-40k	经验3-5年 / 本科	美团点评thttps://www.lagou.com/gongsi/50702.html	移动互联网,O2O / D轮及以上
java 5年	[望京]	08:57发布	25k-35k	经验5-10年 / 本科	丽兹行thttps://www.lagou.com/gongsi/124195.html	企业服务,广告营销 / 不需要融资
Java开发工程师	[中关村]	1天前发布	16k-23k	经验3-5年 / 本科	新东方在线thttps://www.lagou.com/gongsi/9933.html	移动互联网,教育 / 不需要融资
Java开发工程师	[大屯]	1天前发布	15k-25k	经验5-10年 / 本科	奥琦玮thttps://www.lagou.com/gongsi/28659.html	移动互联网,O2O / B轮
java开发工程师	[酒仙桥]	2017-12-22	18k-30k	经验3-5年 / 本科	蓝信移动(北京)科技有限公司thttps://www.lagou.com/gongsi/27298.html	移动互联网 / B轮
Java开发工程师	[北京]	1天前发布	15k-25k	经验3-5年 / 本科	Trusfort芯盾时代thttps://www.lagou.com/gongsi/97399.html	移动互联网,信息安全 / B轮
JAVA	[太阳宫]	1天前发布	20k-30k	经验5-10年 / 本科	育学园thttps://www.lagou.com/gongsi/53813.html	移动互联网,生活服务 / C轮
Java工程师	[亮马桥]	1天前发布	30k-50k	经验5-10年 / 本科	中信云网thttps://www.lagou.com/gongsi/147548.html	移动互联网,金融 / 不需要融资
Java开发工程师	[西北旺]	1天前发布	18k-25k	经验3-5年 / 本科	深思数盾thttps://www.lagou.com/gongsi/21190.html	信息安全,移动互联网 / 不需要融资
Java	[西直门]	1天前发布	25k-35k	经验5-10年 / 本科	Udesk-企业级智能客服平台thttps://www.lagou.com/gongsi/25439.html	其他 / B轮
java	[望京]	2天前发布	20k-35k	经验5-10年 / 本科	思源科技thttps://www.lagou.com/gongsi/82069.html	移动互联网,社交网络 / 不需要融资
JAVA工程师	[上地]	2017-12-20	15k-20k	经验5-10年 / 大专	青柠创享thttps://www.lagou.com/gongsi/29663.html	移动互联网 / 不需要融资
Java开发工程师	[立水桥]	1天前发布	20k-30k	经验5-10年 / 本科	闲徕互娱thttps://www.lagou.com/gongsi/124192.html	游戏 / 不需要融资
Java开发工程师	[太阳宫]	1天前发布	20k-30k	经验3-5年 / 本科	蜜芽thttps://www.lagou.com/gongsi/9127.html	电子商务 / D轮及以上
Java	[海淀区]	1天前发布	30k-50k	经验3-5年 / 本科	搜狐媒体thttps://www.lagou.com/gongsi/106303.html	移动互联网,广告营销 / 上市公司
Java	[海淀区]	1天前发布	30k-50k	经验3-5年 / 本科	搜狐媒体thttps://www.lagou.com/gongsi/106303.html	移动互联网,广告营销 / 上市公司
Java开发工程师	[苏州街]	1天前发布	15k-30k	经验3-5年 / 本科	拉勾网thttps://www.lagou.com/gongsi/147.html	企业服务,招聘 / D轮及以上
Java软件工程师(Base南京)	[知春路]	2天前发布	15k-30k	经验5-10年 / 本科	vivothttps://www.lagou.com/gongsi/19875.html	移动互联网 / 未融资
Java开发工程师	[学院路]	1天前发布	15k-22k	经验3-5年 / 本科	北京智慧图thttps://www.lagou.com/gongsi/1337.html	移动互联网,数据服务 / C轮
Java工程师	[广渠门]	1天前发布	15k-25k	经验3-5年 / 本科	家园网thttps://www.lagou.com/gongsi/53137.html	O2O / 不需要融资
java研发工程师	[东四]	1天前发布	15k-20k	经验3-5年 / 本科	集奥聚合(GEO)thttps://www.lagou.com/gongsi/13179.html	数据服务 / B轮
JAVA	[望京]	08:57发布	15k-30k	经验3-5年 / 本科	丽兹行thttps://www.lagou.com/gongsi/124195.html	企业服务,广告营销 / 不需要融资
资深Java研发工程师	[望京]	02:17发布	35k-45k	经验3-5年 / 本科	美团点评thttps://www.lagou.com/gongsi/50702.html	移动互联网,O2O / D轮及以上
JAVA工程师	[中关村]	1天前发布	20k-30k	经验3-5年 / 本科	易快报thttps://www.lagou.com/gongsi/45121.html	移动互联网,企业服务 / A轮
Java开发工程师	[望京]	1天前发布	15k-28k	经验3-5年 / 本科	Coupangthttps://www.lagou.com/gongsi/49363.html	移动互联网,O2O / 不需要融资
Java开发工程师	[大望路]	1天前发布	20k-35k	经验3-5年 / 硕士	宜信thttps://www.lagou.com/gongsi/3786.html	金融 / 上市公司
java	[中关村]	1天前发布	15k-25k	经验3-5年 / 大专	亿迅科技thttps://www.lagou.com/gongsi/34798.html	游戏 / C轮
Java工程师	[亦庄]	1天前发布	18k-30k	经验3-5年 / 本科	京东商城thttps://www.lagou.com/gongsi/139755.html	电子商务 / 上市公司
Java开发工程师	[北京]	2天前发布	20k-40k	经验5-10年 / 本科	京东金融thttps://www.lagou.com/gongsi/534.html	金融 / 上市公司


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: