python爬取微博热搜
时间: 2023-10-05 22:13:33 浏览: 112
要使用Python爬取微博热搜,可以通过以下步骤实现:
1. 导入所需的库:
```python
import requests
from bs4 import BeautifulSoup
```
2. 发送HTTP请求并获取网页的HTML内容:
```python
response = requests.get('https://s.weibo.com/top/summary?cate=realtimehot')
html = response.text
```
3. 使用BeautifulSoup解析HTML内容:
```python
soup = BeautifulSoup(html, 'html.parser')
```
4. 定位热搜榜单的元素,提取热搜信息:
```python
hot_list = soup.find_all('tr', class_='')
for hot in hot_list:
hot_text = hot.find('td', class_='td-02').text.strip()
print(hot_text)
```
完整的示例代码如下所示:
```python
import requests
from bs4 import BeautifulSoup
response = requests.get('https://s.weibo.com/top/summary?cate=realtimehot')
html = response.text
soup = BeautifulSoup(html, 'html.parser')
hot_list = soup.find_all('tr', class_='')
for hot in hot_list:
hot_text = hot.find('td', class_='td-02').text.strip()
print(hot_text)
```
阅读全文