爬取百度前20个热搜

import requests
from bs4 import BeautifulSoup
import bs4
import pandas as pd
url = 'http://top.baidu.com/buzz?b=341&c=513&fr=topbuzz_b341_c513'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/69.0.3497.100 Safari/537.36'}
r=requests.get(url)
r.encoding=r.apparent_encoding
html=r.text
soup=BeautifulSoup(html,'lxml')
a=[]
b=[]
for link1 in soup.find_all(class_='list-title'):
    a.append(link1.get_text())
for link2 in soup.find_all('td',class_='last'):
    b.append(link2.get_text().strip())
data=pd.DataFrame([a,b],index=["关键词","搜索指数"]).T
print("爬取百度热搜榜前十:","\n")
print(data.iloc[0:20])

爬取百度前20个热搜_第1张图片

 

你可能感兴趣的:(爬取百度前20个热搜)