Python 利用多线程进行爬虫(一)
这几天帮同学爬取拉勾网的招聘信息,真好学习一下多线程爬虫,之前做过几次爬虫都是单线程的。看到网上说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 queueurl = 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 doneself.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 chunkf.write(res.encode('utf-8'))#signals to queue job is doneself.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 金融 / 上市公司
Python 利用多线程进行爬虫(一)相关推荐
- python多线程爬虫实例-python支持多线程的爬虫实例
python是支持多线程的, 主要是通过thread和threading这两个模块来实现的,本文主要给大家分享python实现多线程网页爬虫 一般来说,使用线程有两种模式, 一种是创建线程要执行的函数 ...
- python利用多线程批量下载高清美女图片(350秒下载近3600张1.2个G的照片,地址可变)
目录 第一章.前言 1.1.实现的效果: 1.2.需要用到的库: 第二章.代码分块讲解 2.1.对象的定义和初始化 2.2.方法1和2获取所有图集链接 2.2.1. 对应网站结构 2.2.2 .相应代 ...
- python+Selenium多线程后台爬虫例子
Selenium多线程后台爬虫 一.前言: 有些网站不支持网页源码爬虫.或要爬取的网页内容不在网页源码中, 等需要使用Selenium进行爬虫 二.准备工作: 安装selenium及对应googlec ...
- python利用proxybroker构建爬虫免费IP代理池!不用担心被封了!
大纲 前言 ProxyBroker简介 ProxyBroker安装 在终端使用ProxyBroker 在代码中使用ProxyBroker 总结 前言 写爬虫的小伙伴可能遇到过这种情况: 正当悠闲地喝着 ...
- Python《多线程并发爬虫》
今天再去爬取另外一个网站 http://pic.netbian.com/ 先来看看这个网站的几张图片,我们试图单独爬取看看. 我们单独爬取一下试一试 本地查看,证明图片是可以爬取成功的. 代码如下: ...
- python利用requests进行爬虫_Python利用Requests库写爬虫(一)
基本Get请求: #-*- coding:utf-8 -*- import requests url = 'http://www.baidu.com' r = requests.get(url) pr ...
- python 多线程同步_Python利用多线程同步锁实现多窗口订票系统(推荐)
利用Python实现多窗口订票系统,利用 threading.Lock() 避免出现一票多卖,无票也卖的情况,并规范化输出情况. 代码: import threading import time ti ...
- python爬取百度贴吧中的所有邮箱_使用 Python 编写多线程爬虫抓取百度贴吧邮箱与手机号...
原标题:使用 Python 编写多线程爬虫抓取百度贴吧邮箱与手机号 不知道大家过年都是怎么过的,反正栏主是在家睡了一天,醒来的时候登QQ发现有人找我要一份贴吧爬虫的源代码,想起之前练手的时候写过一个抓 ...
- python多线程爬虫实例-Python实现多线程爬虫
编辑推荐: 本文主要介绍对Python多线程爬虫实战的整体的思路,希望对大家有帮助. 本文来知乎,由火龙果软件Alice编辑,推荐. 最近在写爬虫程序爬取亚马逊上的评论信息,因此也自学了很多爬虫相关的 ...
最新文章
- page分页php,Page分页函数
- 避免在JSP中写java代码
- MSP430程序库五SPI同步串行通信
- centos radius mysql_FreeRadius2 MySQL配置
- mysql5717开发设置怎么调回来_Window 下安装Mysql5.7.17 及设置编码为utf8的方法
- 学计算机辐射,离散数学对计算机专业系统知识辐射作用.doc
- 并发编程---死锁||递归锁---信号量---Event事件---定时器
- xFire入门(eclipse、tomcat环境)
- OpenCV算法精解1--OpenCV中C++基本操作1
- 架构之美 | 按图索骥,就能做好架构图!
- 彻底关闭win10自动更新
- sci论文发表的难度高吗
- CTF之web学习记录 -- 命令注入
- Cadence Allegro PCB添加泪滴的方法
- 电视直播加点播,800+直播频道全网影视资源
- iFunk翼超极本亲测心得
- python趣味案例编程 Pdf,小小的python编程故事
- Taro 牵手腾讯有数,助力小程序数据化运营
- 主流日志管理解决方案对比
- Java假期班泰安_泰安2019年放假时间表出炉! 春节居然这样放假