借助chatgpt用python抓取微博熱搜數(shù)據(jù)
之前分享過很多chatgpt的替代品2023 年8月還能怎么用 ChatGPT ??,今天就借助chatgpt用python抓取微博熱搜。
問題:使用python3抓取微博熱搜數(shù)據(jù),地址是https://s.weibo.com/top/summary/?,需要標(biāo)題和鏈接 。
chatgpt的回答:
抓取網(wǎng)頁(yè)數(shù)據(jù)可以使用Python的requests庫(kù)和BeautifulSoup庫(kù)。確保你已經(jīng)安裝了這些庫(kù)后,可以按照以下步驟獲取微博熱搜數(shù)據(jù):

很快一個(gè)抓取代碼就寫好了,順便分享下https://www.programming-helper.com/generate-function這個(gè)網(wǎng)站可以根據(jù)描述生成代碼,支持Python,go,php等語(yǔ)言。

生成的代碼需要微調(diào)下:
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':'自己微博賬號(hào)的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)

最新原創(chuàng)文章:
2023 更新版:蘇生不惑開發(fā)過的那些原創(chuàng)工具和腳本
再次更新:2023批量下載公眾號(hào)文章內(nèi)容/話題/圖片/封面/視頻/音頻,導(dǎo)出文章pdf,文章數(shù)據(jù)含閱讀數(shù)/點(diǎn)贊數(shù)/在看數(shù)/留言數(shù)
微博圖床又搞事情不能用了,盤它,我順便寫了個(gè)微博圖片/視頻/內(nèi)容/文章批量下載工具
2023 年數(shù)字圖書館 zlibrary 復(fù)活,新推出客戶端人人可用
總有人問我 Cookie 是什么?