适合小白
import requests
from bs4 import BeautifulSoup
import openpyxl
url="https://top.baidu.com/board?tab=realtime"
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"
}
response=requests.get(url=url,headers=headers)# 模拟浏览器访问
html=response.content
print(response.status_code)# 输出访问状态
soup=BeautifulSoup(response.text,"html.parser")
data01=[]
for items in soup.find_all('div',{'class':'hot-desc_1m_jR'}): #热搜内容
data01.append(items.text)
workd=openpyxl.Workbook()
sheet=workd.active
sheet.title="热搜内容"
for j in range(len(data01)):
sheet.cell(row=j+2,column=1,value=data01[j])
workd.save("热搜内容.xlsx")