Python爬虫案例-头条热搜

爬虫代码

import requests
import time
def get_toutiao_hot():
    while True:
        url = "https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc"
        resp = requests.get(url)
        resp.encoding = 'utf-8'
        resp = resp.json()
        news_ls = []
        i = 0
        news = resp.get('data')
        news.reverse()
        for new in news:
            i += 1
            print(('\033[1;37m'+str(i)+'\033[0m').center(50,'*'))
            news_ls.append({'title':new.get('Title'),'url':new.get('Url')})
            print('\033[1;36m'+new.get('Title')+'\033[0m')
        fixed_top_data = resp.get('fixed_top_data')
        fixed_top_data = fixed_top_data[0]
        news_ls.append({'title':fixed_top_data.get('Title'),'url':fixed_top_data.get('Url')})
        print(('\033[1;37m'+str(i+1)+'\033[0m').center(50,'*'))
        print('\033[1;36m'+news_ls[-1].get('title')+'\033[0m')
        user_input = input("输入新闻编号获取进一步访问的超链接,输入q/Q退出,输入r/R刷新热榜:")
        if user_input == 'q' or user_input == 'Q':
            break
        elif user_input == 'r' or user_input == 'R':
            continue
        elif user_input in [str(i) for i in range(1,len(news_ls)+1)]:
            news_index = eval(user_input) - 1
            print(news_ls[news_index].get('url'))
            print("\033[1;33m" + "按住Ctrl键,点击超链接进行访问" + "\033[0m")
            print('\033[5;31m'+'10s后自动刷新热榜'+'\033[0m')
            time.sleep(10)
            continue
        else:
            print("Invalid User Input.")
            print('\033[5;31m'+"3s后自动刷新热榜"+'\033[0m')
            time.sleep(3)
            continue
    print("Over,退出头条热搜!")
if __name__ == '__main__':
    get_toutiao_hot()

爬虫结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

数智侠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值