python爬虫中多线程的使用详解
作者:记住我忘记我 时间:2021-01-18 09:16:15
queue介绍
queue是python的标准库,俗称队列.可以直接import引用,在python2.x中,模块名为Queue。python3直接queue即可
在python中,多个线程之间的数据是共享的,多个线程进行数据交换的时候,不能够保证数据的安全性和一致性,所以当多个线程需要进行数据交换的时候,队列就出现了,队列可以完美解决线程间的数据交换,保证线程间数据的安全性和一致性。
#多线程实战栗子(糗百)
#用一个队列Queue对象,
#先产生所有url,put进队列;
#开启多线程,把queue队列作为参数传入
#主函数中读取url
import requests
from queue import Queue
import re,os,threading,time
# 构造所有ip地址并添加进queue队列
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
urlQueue = Queue()
[urlQueue.put('http://www.qiumeimei.com/image/page/{}'.format(i)) for i in range(1,14)]
def get_image(urlQueue):
while True:
try:
# 不阻塞的读取队列数据
url = urlQueue.get_nowait()
# i = urlQueue.qsize()
except Exception as e:
break
print('Current Thread Name %s, Url: %s ' % (threading.currentThread().name, url))
try:
res = requests.get(url, headers=headers)
url_infos = re.findall('data-lazy-src="(.*?)"', res.text, re.S)
for url_info in url_infos:
if os.path.exists(img_path + url_info[-20:]):
print('图片已存在')
else:
image = requests.get(url_info, headers=headers)
with open(img_path + url_info[-20:], 'wb') as fp:
time.sleep(1)
fp.write(image.content)
print('正在下载:' + url_info)
except Exception as e:
print(e)
if __name__ == '__main__':
startTime = time.time()
# 定义图片存储路径
img_path = './img/'
if not os.path.exists(img_path):
os.mkdir(img_path)
threads = []
# 可以调节线程数, 进而控制抓取速度
threadNum = 4
for i in range(0, threadNum):
t = threading.Thread(target=get_image, args=(urlQueue,))
threads.append(t)
for t in threads:
t.start()
for t in threads:
# 多线程多join的情况下,依次执行各线程的join方法, 这样可以确保主线程最后退出, 且各个线程间没有阻塞
t.join()
endTime = time.time()
print('Done, Time cost: %s ' % (endTime - startTime))
总结
以上所述是小编给大家介绍的python爬虫中多线程的使用详解,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://www.cnblogs.com/nmsghgnv/archive/2019/09/23/11571712.html
标签:python,爬虫,多线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Oracle 8x实现自动断开后再连接
2010-07-26 13:03:00
python matlibplot绘制3D图形
2023-05-21 18:47:36
![](https://img.aspxhome.com/file/2023/4/87724_0s.png)
Python 代码实现列表的最小公倍数
2021-08-11 05:15:24
速记Python布尔值
2022-04-23 10:02:17
在任意字符集下正常显示网页的方法二(续)
2023-11-22 17:36:14
php使用pack处理二进制文件的方法
2023-11-21 04:26:11
Python利用带权重随机数解决抽奖和游戏爆装备问题
2023-09-21 10:16:43
![](https://img.aspxhome.com/file/2023/5/120345_0s.jpg)
Oracle数据库及应用程序优化开发者网络Oracle
2010-07-18 13:02:00
用ASP在线创建Word与Excel文档
2008-07-20 19:17:00
python爬虫把url链接编码成gbk2312格式过程解析
2023-04-26 21:00:10
![](https://img.aspxhome.com/file/2023/8/105698_0s.png)
django+mysql的使用示例
2022-10-24 20:34:15
![](https://img.aspxhome.com/file/2023/9/67259_0s.png)
Python控制台输出俄罗斯方块的方法实例
2023-08-09 16:32:19
如何查询日期类型的数据?
2009-11-11 20:04:00
python+requests+unittest API接口测试实例(详解)
2021-10-14 13:15:08
![](https://img.aspxhome.com/file/2023/4/65124_0s.jpg)
做设计还是做产品
2009-06-11 13:01:00
Python检测PE所启用保护方式详解
2022-03-11 12:36:08
![](https://img.aspxhome.com/file/2023/6/88906_0s.png)
HTTP中header头部信息详解
2023-06-11 23:33:17
初瞥 Google Chrome Frame
2009-10-06 14:41:00
![](https://img.aspxhome.com/file/UploadPic/200910/6/medium-75s.jpg)
ASP连接MySQL数据库代码示例
2010-03-14 11:24:00
Python SSL证书验证问题解决方案
2022-11-06 13:54:35
![](https://img.aspxhome.com/file/2023/6/118056_0s.png)