python爬虫爬取新浪微博热搜榜
时间: 2024-05-30 10:06:59 浏览: 23
Python 爬虫爬取新浪微博热搜榜可以使用 Python 的 requests 库和 BeautifulSoup 库。
具体步骤如下:
1. 发送请求获取网页内容,可以使用 requests 库中的 get() 方法。
2. 解析网页内容,可以使用 BeautifulSoup 库中的 BeautifulSoup() 方法。
3. 定位需要爬取的内容,可以使用 find() 或 find_all() 方法。
4. 提取需要爬取的内容,例如热搜榜的标题、链接、热度等信息。
5. 将提取的内容存储到本地或者数据库中。
下面是示例代码:
```
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')
# 定位需要爬取的内容
hot_list = soup.find_all('td', class_='td-02')
# 提取需要爬取的内容
for hot in hot_list:
title = hot.find('a').text # 热搜标题
link = 'https://s.weibo.com' + hot.find('a')['href'] # 热搜链接
hot_num = hot.find('span').text # 热度
print(title, link, hot_num)
# 将提取的内容存储到本地或者数据库中
# ...
```