爬虫-36kr-使用xpath爬取数据-part1-提取接口所需的6开头的数字-拼接下一个接口的路径

import requests
from lxml import etree

class Spider():
    def __init__(self):
        # 起始页
        self.start_url = "https://36kr.com/"
        # 请求头
        self.headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"}
        # 域名
        self.host = "https://36kr.com"
    def start(self):
        # 爬取起始页
        # 第一页HTML = 爬取起始页的方法
        index_html = self.parse_url(self.start_url)
        print(index_html)

        # 获取下一页内容
        # 下一页url = 获取下一页内容(第一页HTML)
        eobj = etree.HTML(index_html)
        res = eobj.xpath('//a[text()="查看更多资讯"]/@href')[0]
        print(res)
        next_url = self.host + res

        # 第一次点击 获取更多
        first_more_html = self.parse_url(next_url)
        # first_more_html 就是 https://36kr.com/information/web_news 页面的html代码
        # 所有的文章容器div //div[@class="information-flow-item"]
        # 最后一个文章的div  //div[@class="information-flow-item"][last()]

        # 第一次更看更多的,列表html页的对象
        first_more_obj = etree.HTML(first_more_html)
        last_div = first_more_obj.xpath('//div[@class="information-flow-item"][last()]')[0]
        print(last_div)

        anchor_div_class = last_div.xpath("./div[1]/@class")[0]
        # anchor - 69141
        print(anchor_div_class)

        # 取69141
        import re
        b_id = re.search(r"\d+", anchor_div_class).group()
        # 匹配成功的结果对象.group()
        print(b_id)

        # 接口的拼接完毕
        next_data_api = "https://36kr.com/pp/api/aggregation-entity?type=web_latest_article&b_id={}&per_page=30".format(b_id)
        print(next_data_api)

        # 下一个接口的数据
        # 直接从next_data_api中取最后一个新闻的id字段
        # 用id字段对应的6什么什么开头的值,可以拼出下一个接口


        # 思路:正则拿
        # 正则式的写法: re.compile(r'<a class="kr-home-flow-see-more" href="(.*?)">查看更多资讯</a>')
        # 最终可以得到, "/information/web_news?anchor=68962"
        # 下一页的url   https://36kr.com/information/web_news?anchor=68962
        # https://36kr.com/information/web_news?anchor=68957

        # 第三页的内容
        # 发一个ajax请求
        # https://36kr.com/pp/api/aggregation-entity?type=web_latest_article&b_id=68892&per_page=30
        # https://36kr.com/pp/api/aggregation-entity?type=web_latest_article&b_id=68848&per_page=30

        # 推理出第四页的内容
        # https://36kr.com/pp/api/aggregation-entity?type=web_latest_article&b_id=68847&per_page=30

        # 第五页的内容
        # https://36kr.com/pp/api/aggregation-entity?type=web_latest_article&b_id=68817&per_page=30

    def parse_url(self, url):
        res = requests.get(url, headers=self.headers)
        return res.content.decode()


s = Spider()
s.start()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鲸鱼编程pyhui

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

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

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

打赏作者

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

抵扣说明:

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

余额充值