python爬取微博热搜数据
时间: 2023-08-26 12:06:25 浏览: 85
可以使用Python中的requests库和BeautifulSoup库来实现爬取微博热搜数据的功能。
首先,需要使用requests库向微博热搜页面发送请求,并获取返回的HTML代码。代码如下:
```python
import requests
url = 'https://s.weibo.com/top/summary?cate=realtimehot'
response = requests.get(url)
html = response.text
```
接下来,需要使用BeautifulSoup库解析HTML代码,并提取需要的数据。可以使用find_all()方法找到热搜榜单中的每一个条目,并从中提取热搜词和热度值。代码如下:
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('tr', class_='')
for item in items:
title = item.find('a').text
hotness = item.find('span', class_='hot').text
print(title, hotness)
```
完整代码如下:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://s.weibo.com/top/summary?cate=realtimehot'
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('tr', class_='')
for item in items:
title = item.find('a').text
hotness = item.find('span', class_='hot').text
print(title, hotness)
```
注意:在爬取数据时需要注意尊重网站的robots协议,不要频繁请求网站,否则可能会被网站封禁IP。
阅读全文
相关推荐














