爬取微博热搜

1、打开微博网站:https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6

2、鼠标右击打开检索

3、导入相应的库

4、编写如下代码:

import requests
from bs4 import BeautifulSoup
import pandas as pd
from pandas import DataFrame
ur1='https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6'
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(ur1,headers=headers)#请求网站
r.encoding=r.apparent_encoding#统一编码
data=r.text
soup=BeautifulSoup(data,'html.parser')#使用“美味的汤”工具
print(soup.prettify())#按照标准缩进格式的结构输出
title=[]
content=[]
for i in soup.find_all(class_="title"):#把标题放入空列表
    title.append(i.get_text().strip())
for j in soup.find_all(class_="content"):#把内容放入空列表
   content.append(j.get_text().strip())
html=pd.DataFrame(data,index=["关键词","热度"])

print(html)

 

 

posted @ 2020-03-20 17:40  吕妍  阅读(847)  评论(0)    收藏  举报