python爬取微博想要的评论
时间: 2023-09-12 15:04:32 浏览: 118
python爬虫爬取微博评论案例详解
5星 · 资源好评率100%
要爬取微博的评论,可以使用Python中的第三方库进行实现。常用的库有requests、BeautifulSoup、selenium等。下面是一个简单的示例代码,来实现爬取指定微博的评论:
```python
import requests
from bs4 import BeautifulSoup
# 定义要爬取的微博链接和评论页数
weibo_url = 'https://weibo.com/1234567890/ABCDEFGHI?type=comment'
page_count = 3
# 模拟请求头,加上User-Agent信息
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 发送请求,获取页面内容
response = requests.get(weibo_url, headers=headers)
html = response.text
# 使用BeautifulSoup解析页面
soup = BeautifulSoup(html, 'html.parser')
# 定位评论所在div标签
comment_divs = soup.find_all('div', class_='list_li S_line1 clearfix')
# 遍历每个评论div,提取评论内容
comments = []
for comment_div in comment_divs:
comment = comment_div.find('span', class_='ctt').text.strip()
comments.append(comment)
# 打印评论内容
for i, comment in enumerate(comments):
print(f'评论{i+1}: {comment}')
```
上述代码中,需要替换weibo_url为要爬取的微博链接,page_count为要爬取的评论页数。运行代码后,就可以获取到微博的评论内容。<span class="em">1</span>
#### 引用[.reference_title]
- *1* [python爬取微博视频](https://download.csdn.net/download/qw1540235670/10297759)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文