Python爬虫采集,中介网互联网网站排行榜, 样本数量:58341

举报
梦想橡皮擦 发表于 2022/01/17 13:57:42 2022/01/17
【摘要】 今天要实现的是《爬虫120例》中的第28例,采用的技术方案为多线程+队列。 目标站点分析本次要抓取的目标站点为:中介网,这个网站提供了网站排行榜、互联网网站排行榜、中文网站排行榜等数据。网站展示的样本数据量是 :58341。采集页面地址为 https://www.zhongjie.com/top/rank_all_1.html,UI如下所示:由于页面存在一个【尾页】超链接,所以直接通过该超链...

今天要实现的是《爬虫120例》中的第28例,采用的技术方案为多线程+队列。

目标站点分析

本次要抓取的目标站点为:中介网,这个网站提供了网站排行榜、互联网网站排行榜、中文网站排行榜等数据。

网站展示的样本数据量是 :58341。

采集页面地址为 https://www.zhongjie.com/top/rank_all_1.html,UI如下所示:
image.png

由于页面存在一个【尾页】超链接,所以直接通过该超链接获取累计页面即可。

其余页面遵循简单分页规则:

https://www.zhongjie.com/top/rank_all_1.html
https://www.zhongjie.com/top/rank_all_2.html

基于此,本次Python爬虫的解决方案如下,页面请求使用 requests 库,页面解析使用 lxml,多线程使用 threading 模块,队列依旧采用 queue 模块。

编码时间

在正式编码前,先通过一张图将逻辑进行梳理。

本爬虫编写步骤文字描述如下:

  1. 预先请求第一页,解析出总页码;
  2. 通过生产者不断获取域名详情页地址,添加到队列中;
  3. 消费者函数从队列获取详情页地址,解析目标数据。

image.png

总页码的生成代码非常简单

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()
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。