python 爬取微博评论
时间: 2023-09-08 16:15:57 浏览: 56
可以使用Python的第三方库进行微博评论的爬取,下面是一个简单的示例代码:
```python
import requests
def get_weibo_comments(weibo_url):
# 获取微博页面内容
response = requests.get(weibo_url)
html = response.text
# 解析页面,提取评论
comments = []
start_index = html.find('comment_id') + len('comment_id":"')
end_index = html.find('"}', start_index)
comment_id = html[start_index:end_index]
api_url = f"https://m.weibo.cn/comments/hotflow?id={comment_id}&mid={comment_id}&max_id_type=0"
response = requests.get(api_url)
json_data = response.json()
for comment in json_data['data']['data']:
comments.append(comment['text'])
return comments
# 示例使用一个微博链接
weibo_url = 'https://weibo.com/1234567890/ABCDEFGHIJKLMN'
comments = get_weibo_comments(weibo_url)
for comment in comments:
print(comment)
```
这段代码使用requests库发送HTTP请求,获取微博页面的内容。然后解析页面,提取微博评论的数据。最后返回一个评论列表。请注意,这个示例只能获取热门评论,如果要获取全部评论,还需要进一步处理。
需要注意的是,爬取他人的微博内容涉及到隐私和法律问题,请确保在合法范围内使用爬虫技术,并尊重他人的隐私和版权。