1 Star 0 Fork 1

ShawnChenRTZ/hot_billboard

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
weibo_hot.py 1.80 KB
一键复制 编辑 原始数据 按行查看 历史
ShawnChenRTZ 提交于 2023-10-24 17:05 . 隐藏请求头的cokkie敏感信息
import requests
import time
from bs4 import BeautifulSoup
def get_weibo_hot():
while True:
url = "https://s.weibo.com/top/summary?cate=realtimehot"
headers = {"Cookie":"SUB=_2AkMT0zixf8NxqwJRmP4RzG3iaIxxyQnEilj8lqJRMxHRl-yT9kqm0dtRB6OFMWXlM5SjftExkMQK6NASTHqZWXWFEB;"}
resp = requests.get(url=url,headers=headers)
resp.encoding = 'utf-8'
html = resp.text
soup = BeautifulSoup(html,'html.parser')
news = soup.findAll(class_='td-02')
news.reverse()
base_url = "https://s.weibo.com"
news_ls = []
i = 0
for new in news:
i = i + 1
url = base_url + new.find('a').attrs['href']
# print(url)
title = new.find('a').text
print(('\033[1;37m' + str(i) + '\033[0m').center(50,'*'))
print('\033[1;36m' + title + '\033[0m')
news_ls.append({"title":title,"url":url})
news_length = len(news_ls)
# news_ls.reverse()
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,news_length+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,退出微博热搜!")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/shawn_chen_rtz/hot_billboard.git
git@gitee.com:shawn_chen_rtz/hot_billboard.git
shawn_chen_rtz
hot_billboard
hot_billboard
master

搜索帮助