python爬虫(二) 之 42号网汽车文章爬虫

python爬虫(二) 之 42号网汽车文章爬虫

今天在咸鱼上有个买家找我一个42号网汽车文章的爬虫,目前需求已经做完了,现在将这部分代码开源,供大家参考。爬虫能够抓取到网站上所有文章的数据,大概一小时左右就能将这个网站上的数据吃干抹尽。

python 复制代码
import requests
import json
import csv
from lxml import etree
import time


class How42:

    def __init__(self):
        self.article_list_pre_url = "https://api.42how.com/article?page="
        self.article_list_post_url = "&pageSize=10&orderBy=createTime&order=DESC&isProfessional=true&userType=0"
        self.start_page = 1
        self.end_page = 1000
        self.payload = {}
        self.article_list_headers = {
            'authority': 'api.42how.com',
            'accept': 'application/json, text/plain, */*',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'no-cache',
            'cookie': '_ga_6GM2YNVSMY=GS1.1.1710298637.1.0.1710298637.60.0.0; _ga=GA1.1.383334843.1710298637',
            'origin': 'https://www.42how.com',
            'pragma': 'no-cache',
            'referer': 'https://www.42how.com/',
            'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-site',
            'source-type': '42web',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
        }

        self.article_detail_headers = {
            'authority': 'www.42how.com',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'no-cache',
            'cookie': 'i18n_redirected=zh; _ga=GA1.1.383334843.1710298637; _ga_6GM2YNVSMY=GS1.1.1710302704.2.0.1710302704.60.0.0',
            'pragma': 'no-cache',
            'referer': 'https://www.42how.com/?l=article',
            'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-site': 'same-origin',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
        }

    def get_request(self, url, headers):
        response = requests.request("GET", url, headers=headers, data=self.payload)
        return response.text

    def do_work(self):
        with open('42号.csv', 'w', newline='', encoding='utf-8-sig') as file:
            writer = csv.writer(file)
            csv_title = ["标题", "作者", "发布时间", "原文地址", "正文"]
            writer.writerow(csv_title)

            for current_page in range(self.start_page, self.end_page):
                print("================> 当前第" + str(current_page) + "页,共" + str(self.end_page) + "页 ============")
                article_list_url = self.article_list_pre_url + str(current_page)
                text = self.get_request(article_list_url, headers=self.article_list_headers)
                data = json.loads(text)["data"]
                self.write_page(writer, data)

    def write_page(self, writer, data):
        for item in data:
            # print(item["title"])
            # print(item["author"]["username"])
            # print(item["created_at"])
            # 获取文章详情内容
            # https://www.xchuxing.com/article/116378
            article_url = "https://www.42how.com/article/" + str(item["id"])
            text = self.get_request(article_url, headers=self.article_detail_headers)

            html = etree.HTML(text)
            # //*[@id="nice"]/div/div[1]
            result = html.xpath('normalize-space(//*[@id="nice"]/div/div[1])')
            print(result)
            # print(result)
            # time_struct = time.localtime(item["created_at"])
            # date = time.strftime("%Y-%m-%d %H:%M:%S", time_struct)

            row = [item["title"], item["author"]["nickname"], article_url, item["createTime"], result]
            writer.writerow(row)
            print("===========> 当前文章 " + article_url + " 写入完毕", )


if __name__ == '__main__':
    how42 = How42()
    how42.do_work()

下面是程序的运行结果,最终抓取的数据放在同级目录下的42号.csv文件。

相关推荐
Juchecar21 分钟前
分析:将现代开源浏览器的JavaScript引擎更换为Python的可行性与操作
前端·javascript·python
科大饭桶23 分钟前
昇腾AI自学Day2-- 深度学习基础工具与数学
人工智能·pytorch·python·深度学习·numpy
天才测试猿2 小时前
常见的Jmeter压测问题
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·压力测试
mortimer2 小时前
一次与“顽固”外部程序的艰难交锋:subprocess 调用exe踩坑实录
windows·python·ai编程
来自天蝎座的孙孙4 小时前
洛谷P1595讲解(加强版)+错排讲解
python·算法
张子夜 iiii5 小时前
机器学习算法系列专栏:主成分分析(PCA)降维算法(初学者)
人工智能·python·算法·机器学习
跟橙姐学代码6 小时前
学Python像学做人:从基础语法到人生哲理的成长之路
前端·python
Keying,,,,6 小时前
力扣hot100 | 矩阵 | 73. 矩阵置零、54. 螺旋矩阵、48. 旋转图像、240. 搜索二维矩阵 II
python·算法·leetcode·矩阵
桃源学社(接毕设)6 小时前
基于人工智能和物联网融合跌倒监控系统(LW+源码+讲解+部署)
人工智能·python·单片机·yolov8
yunhuibin7 小时前
pycharm2025导入anaconda创建的各个AI环境
人工智能·python