Python3多线程处理爬虫的实战
作者:我才是李叔 时间:2023-08-16 02:16:21
多线程
到底什么是多线程?说起多线程我们首先从单线程来说。例如,我在这里看书,等这件事情干完,我就再去听音乐。对于这两件事情来说都是属于单线程,是一个完成了再接着完成下一个。但是我一般看书一边听歌,同时进行,这个就属于多线程了。
在爬虫过程中,如果只使用单线程进行爬取,效率会比较低下,因此多线程的爬虫处理方式更为常用。Python3提供了threading模块来支持多线程编程,以下是使用Python3多线程处理爬虫的一般步骤:
导入依赖模块
import threading
import requests
from queue import Queue
构建爬虫类
class Spider:
def __init__(self):
self.urls = Queue() # 待爬取的链接队列
self.results = [] # 存储爬取结果的列表
self.lock = threading.Lock() # 线程锁
self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 获取链接列表
def get_urls(self):
# 这里可以从文件、数据库、网页等方式获取待爬取的链接
# 这里以一个示例链接列表作为例子
urls = ['<https://www.example.com/page1>', '<https://www.example.com/page2>', '<https://www.example.com/page3>']
for url in urls:
self.urls.put(url)
# 爬取页面并处理结果
def crawl(self):
while not self.urls.empty():
url = self.urls.get()
try:
response = requests.get(url, headers=self.headers)
# 这里可以对response进行解析,获取需要的信息
# 这里以抓取页面title作为例子
title = response.text.split('<title>')[1].split('</title>')[0]
self.results.append(title)
except Exception as e:
print(e)
finally:
self.urls.task_done()
# 启动多线程爬虫
def run(self, thread_num=10):
self.get_urls()
for i in range(thread_num):
t = threading.Thread(target=self.crawl)
t.start()
self.urls.join()
# 将结果写入文件或者数据库
with self.lock:
with open('result.txt', 'a') as f:
for result in self.results:
f.write(result + '\\n')
来源:https://blog.csdn.net/qq_56920529/article/details/129201627
标签:Python3,多线程爬虫
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Matplotlib控制坐标轴刻度间距与标签实例代码
2022-01-26 03:47:39
![](https://img.aspxhome.com/file/2023/4/113124_0s.png)
python中_del_还原数据的方法
2022-07-28 18:49:14
mysql 5.7.23 安装配置方法图文教程
2024-01-14 02:58:36
![](https://img.aspxhome.com/file/2023/3/119713_0s.jpg)
SQL Server 比较日期大小的方法
2024-01-22 16:03:03
![](https://img.aspxhome.com/file/2023/5/99955_0s.jpg)
简单掌握Python的Collections模块中counter结构的用法
2023-05-17 00:20:13
将ASP纪录集输出成n列表格的方法
2008-03-19 13:27:00
Python爬虫之正则表达式基本用法实例分析
2022-12-20 17:02:16
![](https://img.aspxhome.com/file/2023/1/75361_0s.png)
SQLite Delete详解及实例代码
2024-01-12 17:15:46
教你怎么用python连接sql server
2023-01-31 22:27:33
![](https://img.aspxhome.com/file/2023/2/124642_0s.png)
Python 获取当前路径3种方法
2023-04-15 00:03:24
![](https://img.aspxhome.com/file/2023/7/127667_0s.png)
Go语言通过smtp发送邮件的方法
2024-01-29 19:21:27
node.js微信公众平台开发教程
2024-05-03 15:56:19
![](https://img.aspxhome.com/file/2023/3/129353_0s.png)
在VScode中引用自定义模块问题
2023-08-13 03:50:37
![](https://img.aspxhome.com/file/2023/3/111943_0s.png)
Python中非常好用的内置函数详解
2023-04-05 06:28:25
![](https://img.aspxhome.com/file/2023/0/70080_0s.png)
一文详解前端进阶之IntersectionObserver
2024-05-03 15:07:48
![](https://img.aspxhome.com/file/2023/0/130520_0s.png)
Pandas常用的数据结构和常用的数据分析技术
2021-02-10 05:22:35
JS实现长图上下滚动效果
2023-07-22 10:28:26
![](https://img.aspxhome.com/file/2023/7/55967_0s.gif)
PHP字符串中提取文件名的实例方法
2023-06-12 18:57:32
python实现音乐播放和下载小程序功能
2023-07-03 17:59:03
![](https://img.aspxhome.com/file/2023/5/105855_0s.jpg)
Python 阶乘详解
2022-01-16 13:56:00
![](https://img.aspxhome.com/file/2023/1/66871_0s.png)