python爬虫(四)之九章智算汽车文章爬虫

python爬虫(四)之九章智算汽车文章爬虫

闲来没事就写一条爬虫抓取网页上的数据,现在数据已经抓完,将九章智算汽车文章的爬虫代码分享出来。当前代码采用python编写,可抓取所有文章,攻大家参考。

import requests
import json
import csv
from lxml import etree
import time


class JiuzhangAI:

    def __init__(self):
        self.article_list_pre_url = "http://jiuzhang-ai.com/col.jsp?id=105"

        self.start_page = 1
        self.end_page = 1000

        self.headers = {
            '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': 'max-age=0',
            'Connection': 'keep-alive',
            'Cookie': '_siteStatVisitorType=visitorType_28545812; _siteStatRedirectUv=redirectUv_28545812; _cliid=ui9MLktTy5IU8qQF; _checkSiteLvBrowser=true; _siteStatId=d6211b21-c0af-4f93-a5df-91f51871188d; _siteStatDay=20240313; _siteStatVisit=visit_28545812; _lastEnterDay=2024-03-13; _siteStatReVisit=reVisit_28545812; _reqArgs=; _siteStatVisitTime=1710339315683',
            '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'
        }

        self.article_detail_headers = {
            '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': 'max-age=0',
            'Connection': 'keep-alive',
            'Cookie': '_siteStatVisitorType=visitorType_28545812; _siteStatRedirectUv=redirectUv_28545812; _cliid=ui9MLktTy5IU8qQF; _checkSiteLvBrowser=true; _siteStatId=d6211b21-c0af-4f93-a5df-91f51871188d; _siteStatDay=20240313; _siteStatVisit=visit_28545812; _lastEnterDay=2024-03-13; _siteStatReVisit=reVisit_28545812; _reqArgs=%7B%22args%22%3A%7B%22id%22%3A233%7D%2C%22anchor%22%3A%22_np%3D0_633_7%22%2C%22type%22%3A10%7D; _siteStatVisitTime=1710343180119',
            '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 post_request(self, url):
        # response = requests.request("GET", url, headers=headers, data=payload)
        # return response.text
        pass

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

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

            text = requests.get(self.article_list_pre_url, headers=self.headers).text
            html = etree.HTML(text)
            # tag_hrefs = html.xpath("//div/div[1]/span/a/@href")
            tags = html.xpath("//div/div[1]/span/a")

            # 遍历所有类别
            for tag in tags:
                tag_href = tag.xpath("./@href")[0]
                tag_title = tag.xpath("./text()")[0].replace("\n", "").replace(" ", "")

                # 1. 写第一页
                article_text = requests.get(tag_href, headers=self.headers).text
                articles_html = etree.HTML(article_text)
                article_list_html = articles_html.xpath('//*[@id="newsList31"]/div[@topclassname="top1"]')
                print(tag_href)

                # 遍历所有文章列表
                self.write_page(writer, article_list_html, tag_title)

                # 2. 从2页开始写
                max_page = articles_html.xpath('//span[@class="pageNo"]/a/span/text()')
                if len(max_page) > 0:
                    max_page = int(max_page[0])
                    for current_page in range(2, max_page):
                        article_text = requests.get(tag_href + "&m31pageno=" + current_page, headers=self.headers).text
                        articles_html = etree.HTML(article_text)
                        article_list_html = articles_html.xpath('//*[@id="newsList31"]/div[@topclassname="top1"]')
                        print(tag_href)

                        # 遍历所有文章列表
                        self.write_page(writer, article_list_html, tag_title)

            # page_no = 1
            # pageCallback = self.init_page_callback
            # while True:
            #     print("================> 当前第" + str(page_no) + "页 ============")
            #
            #     text = self.post_request(self.article_list_pre_url)
            #     data = json.loads(text)["data"]
            #     pageCallback = data["pageCallback"]
            #     itemList = data["itemList"]
            #     # self.write_page(writer, itemList)
            #
            #     page_no += 1

    def write_page(self, writer, article_list_html, tag_title):
        for article_html in article_list_html:
            article_href = article_html.xpath("./table/tr/td[@class='newsTitle']/a/@href")[0]
            publish_date = article_html.xpath("./table/tr/td[@class='newsCalendar']/a/text()")[0]
            article_title = article_html.xpath("./@newsname")[0]
            # print(article_href)
            # print(article_title)
            # print(publish_date)

            article_text = requests.get(url=article_href, headers=self.article_detail_headers).text
            article_detail_html = etree.HTML(article_text)
            article_detail_text = article_detail_html.xpath(
                "normalize-space(//div[@class='richContent  richContent0'])")
            article_author = article_detail_html.xpath("//span[@class='newsInfo newsInfo_author']/text()")
            if len(article_author) > 0:
                article_author = article_author[0]
            else:
                article_author = ""
            # print(article_detail_text)
            # print(article_author)

            row = [tag_title, article_title, article_author.replace("作者:", ""), publish_date, article_href,
                   article_detail_text]
            writer.writerow(row)

            print("===========> 当前文章 " + article_href + " 写入完毕", )


if __name__ == '__main__':
    jiuzhangAI = JiuzhangAI()
    jiuzhangAI.do_work()

运行代码在同级目录下会出现一个九章智算.csv文件的,这个就是所有的汽车文章数据啦。

image-20240512233532593

写在最后

代码精选(www.codehuber.com),程序员的终身学习网站已上线!

如果这篇【文章】有帮助到你,希望可以给【JavaGPT】点个赞👍,创作不易,如果有对【后端技术】、【前端领域】感兴趣的小可爱,也欢迎关注❤️❤️❤️ 【JavaGPT】❤️❤️❤️,我将会给你带来巨大的【收获与惊喜】💝💝💝!

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,这是一个关于Python爬虫获取新浪新闻信息的问题。首先,我们需要先了解一下爬虫的基本概念和工作原理。 爬虫是一种自动化程序,可以模拟人类对网站的访问并自动抓取数据。它可以通过网络上的URL链接来获取网页内容,并将其转换成结构化的数据,供后续的数据分析或其他用途。 下面是一个简单的Python爬虫程序,可以用来获取新浪新闻的信息: ```python import requests from bs4 import BeautifulSoup url = 'https://news.sina.com.cn/' r = requests.get(url) r.encoding = 'utf-8' soup = BeautifulSoup(r.text, 'html.parser') news_list = soup.find_all('a', class_='news-item') for news in news_list: print(news.text.strip()) print(news['href']) ``` 这个程序使用了Python的requests库和BeautifulSoup库来获取新浪新闻的信息。首先,我们需要将新浪新闻的URL链接赋值给变量url,然后使用requests库来发送HTTP请求,获取网页内容。接着,我们需要使用BeautifulSoup库来解析网页内容,提取出我们需要的信息。在这个例子中,我们使用了find_all方法来查找所有class为news-item的a标签,并使用循环打印出每个新闻的标题和链接。 当然,这只是一个简单的例子,实际上,爬虫的应用非常广泛,可以用来获取各种类型的数据,例如文本、图像、音频等等。但是,请注意,爬虫的使用需要遵守法律法规和道德规范,不要进行恶意攻击或侵犯他人隐私。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JavaGPT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值