今日头条,有哪些关于游戏的呢?

用Python获取今日头条中关于游戏的相关信息1,代码修改可以随便查找其中的另外分类内容,比如科技,娱乐,体育之类的

import requests

class TouTiao():
    def __init__(self):
    """
    Initialize 
    """
        self.url = "https://www.toutiao.com/api/pc/feed/?category=news_game&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A1654D0AD58C2E6&cp=5DA5FCB27EC67E1&_signature=YkL0GwAAP-qKmNZmAa1p4mJC9A"
        self.header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
                       'x-requested-with': 'XMLHttpRequest',
                       }

    def get_html(self, max_behot_time):
        """
        Get the website html sources
        """
        try:
            response = requests.get(self.url.format(
                max_behot_time), headers=self.header)
        except Exception as e:
            print(e)

        else:
            return response.json()


    def analyse(self, json):
        """
        analyse the html file and get what we need
        """
        data = json.get("data")
        for item in data:
            try:
                content = {
                    "title": item.get("title").replace("\n", "   "),
                    "source": item.get("source"),
                    "abstract": item.get("abstract"),
                    "comments_count": item.get("comments_count"),
                    "group_source": item.get("group_source"),
                    "source_url": "https://www.toutiao.com{}".format(item.get("source_url")),
                }
            except Exception:
                continue
            else:
                for key, item in content.items():
                    print(key, ": ", item)
            print("")
        
        next_one = json.get("next")["max_behot_time"]
        return next_one


    def run(self):
    """
    This function aims at spidering every possible web
    """
        num = 1 
        print("--------------The {} part--------------".format(num))
        json = self.get_html(1571143858)  # 初始的,可自行查找
        next = self.analyse(json)
        print("")
        while next:
            print("--------------The {} part--------------".format(num))
            num += 1
            json = self.get_html(next)
            next = self.analyse(json)
            print("")
            

if __name__ == '__main__':
    test = TouTiao()
    test.run()


  1. 不想写作业,于是有此 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值