爬取贴吧热议榜

import requests
from bs4 import BeautifulSoup
import pandas as pd
titles=[]
hots=[]
url='http://tieba.baidu.com/hottopic/browse/topicList?res_type=1&red_tag=l2774431756'
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.raise_for_status()
r.encoding = r.apparent_encoding
html = r.text
table = BeautifulSoup(html,"html.parser").find("table")
soup=BeautifulSoup(html,'lxml')
for m in soup.find_all(class_="topic-text"):
    titles.append(m.get_text().strip())
for n in soup.find_all(class_="topic-num"):
    hots.append(n.get_text().strip())
final=[titles,hots]
print(final)
s=pd.DataFrame(final,index=["标题","内容数"])
print(s.T)

  

猜你喜欢

转载自www.cnblogs.com/tzq-0716/p/12524668.html