借助chatgpt用python抓取微博热搜数据
之前分享过很多chatgpt的替代品2023 年8月还能怎么用 ChatGPT ? ,今天就借助chatgpt用python抓取微博热搜。
问题:使用python3抓取微博热搜数据,地址是https://s.weibo.com/top/summary/ ,需要标题和链接 。
chatgpt的回答:
抓取网页数据可以使用Python的requests库和BeautifulSoup库。确保你已经安装了这些库后,可以按照以下步骤获取微博热搜数据:

很快一个抓取代码就写好了,顺便分享下https://www.programming-helper.com/generate-function这个网站可以根据描述生成代码,支持Python,go,php等语言。

生成的代码需要微调下:
import requests
from bs4 import BeautifulSoup
url = 'https://s.weibo.com/top/summary/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
'cookie':'自己微博账号的cookie'
}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'lxml')
lis = soup.select('table tbody tr')
for li in lis:
href = 'https://s.weibo.com' + li.select('td a')[0]['href']
title = li.select('td a')[0].get_text()
print(title, href)

最新原创文章:
2023 更新版:苏生不惑开发过的那些原创工具和脚本
再次更新:2023批量下载公众号文章内容/话题/图片/封面/视频/音频,导出文章pdf,文章数据含阅读数/点赞数/在看数/留言数
微博图床又搞事情不能用了,盘它,我顺便写了个微博图片/视频/内容/文章批量下载工具
2023 年数字图书馆 zlibrary 复活,新推出客户端人人可用
总有人问我 Cookie 是什么?