python利用多线程+队列技术爬取中介网互联网网站排行榜
作者:??梦想橡皮擦???? 时间:2023-05-19 08:17:21
目标站点分析
本次要抓取的目标站点为:中介网,这个网站提供了网站排行榜、互联网网站排行榜、中文网站排行榜等数据。
网站展示的样本数据量是 :58341。
采集页面地址为 https://www.zhongjie.com/top/rank_all_1.html
,
UI如下所示:
由于页面存在一个【尾页】超链接,所以直接通过该超链接获取累计页面即可。
其余页面遵循简单分页规则:
https://www.zhongjie.com/top/rank_all_1.html
https://www.zhongjie.com/top/rank_all_2.html
基于此,本次Python爬虫的解决方案如下,页面请求使用 requests
库,页面解析使用 lxml
,多线程使用 threading
模块,队列依旧采用 queue
模块。
编码时间
在正式编码前,先通过一张图将逻辑进行梳理。
本爬虫编写步骤文字描述如下:
预先请求第一页,解析出总页码;
通过生产者不断获取域名详情页地址,添加到队列中;
消费者函数从队列获取详情页地址,解析目标数据。
总页码的生成代码非常简单
def get_total_page():
# get_headers() 函数,可参考开源代码分享数据
res = requests.get(
'https://www.zhongjie.com/top/rank_all_1.html', headers=get_headers(), timeout=5)
element = etree.HTML(res.text)
last_page = element.xpath("//a[@class='weiye']/@href")[0]
pattern = re.compile('(\d+)')
page = pattern.search(last_page)
return int(page.group(1))
总页码生成完毕,就可以进行多线程相关编码,本案例未编写存储部分代码,留给你自行完成啦,
完整代码如下所示:
from queue import Queue
import time
import threading
import requests
from lxml import etree
import random
import re
def get_headers():
uas = [
"Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",
"Mozilla/5.0 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)"
]
ua = random.choice(uas)
headers = {
"user-agent": ua
}
return headers
def get_total_page():
res = requests.get(
'https://www.zhongjie.com/top/rank_all_1.html', headers=get_headers(), timeout=5)
element = etree.HTML(res.text)
last_page = element.xpath("//a[@class='weiye']/@href")[0]
pattern = re.compile('(\d+)')
page = pattern.search(last_page)
return int(page.group(1))
# 生产者
def producer():
while True:
# 取一个分类ID
url = urls.get()
urls.task_done()
if url is None:
break
res = requests.get(url=url, headers=get_headers(), timeout=5)
text = res.text
element = etree.HTML(text)
links = element.xpath('//a[@class="copyright_title"]/@href')
for i in links:
wait_list_urls.put("https://www.zhongjie.com" + i)
# 消费者
def consumer():
while True:
url = wait_list_urls.get()
wait_list_urls.task_done()
if url is None:
break
res = requests.get(url=url, headers=get_headers(), timeout=5)
text = res.text
element = etree.HTML(text)
# 数据提取,更多数据提取,可自行编写 xpath
title = element.xpath('//div[@class="info-head-l"]/h1/text()')
link = element.xpath('//div[@class="info-head-l"]/p[1]/a/text()')
description = element.xpath('//div[@class="info-head-l"]/p[2]/text()')
print(title, link, description)
if __name__ == "__main__":
# 初始化一个队列
urls = Queue(maxsize=0)
last_page = get_total_page()
for p in range(1, last_page + 1):
urls.put(f"https://www.zhongjie.com/top/rank_all_{p}.html")
wait_list_urls = Queue(maxsize=0)
# 开启2个生产者线程
for p_in in range(1, 3):
p = threading.Thread(target=producer)
p.start()
# 开启2个消费者线程
for p_in in range(1, 2):
p = threading.Thread(target=consumer)
p.start()
来源:https://juejin.cn/post/7079964924385968135
标签:python,线程,队列,爬取
0
投稿
猜你喜欢
利用Psyco提升Python运行速度
2021-05-02 19:02:50
python输出指定月份日历的方法
2022-11-27 11:39:18
python读取csv文件指定行的2种方法详解
2022-06-06 13:35:24
GC与JS内存泄露
2010-09-25 19:01:00
pytest fixtures装饰器的使用和如何控制用例的执行顺序
2023-04-11 22:56:09
Python中列表的常用操作详解
2022-07-17 10:16:42
c#将Excel数据导入到数据库的实现代码
2024-01-25 21:53:43
对python使用http、https代理的实例讲解
2022-03-13 00:03:08
对网站内嵌gradio应用的输入输出做审核实现详解
2023-07-22 08:22:05
php编程每天必学之表单验证
2023-07-19 05:50:59
hta应用—代码统计工具
2024-01-31 14:40:44
python实现PyEMD经验模态分解残差量分析
2022-06-22 05:26:17
Django mysqlclient安装和使用详解
2024-01-13 21:13:08
Git标签管理
2022-02-01 18:53:06
简介二分查找算法与相关的Python实现示例
2023-05-13 12:16:45
PHP simplexml_load_file()函数讲解
2023-06-03 23:16:56
Python实现TOPSIS分析法的示例代码
2021-05-09 19:32:47
浅析Python中的赋值和深浅拷贝
2021-09-10 14:30:14
Python async模块使用方法杂谈
2023-12-12 14:46:36
python基于queue和threading实现多线程下载实例
2023-02-04 09:58:22