python爬虫爬取微博评论代码
时间: 2023-08-29 11:05:43 浏览: 286
以下是使用Python爬虫爬取微博评论的代码示例:
```python
import requests
import json
# 设置请求头部信息
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'}
# 设置请求参数
params = {
'id': 'xxxxxx', # 微博ID
'page': 1 # 评论页数
}
# 发送请求获取评论信息
response = requests.get('https://m.weibo.cn/comments/hotflow', headers=headers, params=params)
json_data = json.loads(response.text)
# 解析数据获取评论内容
for comment in json_data['data']['data']:
print(comment['text'])
```
请注意,这个示例代码可能已经过时或不适用于某些特定的情况,因为微博可能会改变API或反爬虫措施。在使用时请注意遵守相关法律法规和网站的使用协议。
相关问题
python爬虫爬取微博评论
要爬取微博评论,可以使用Python中的第三方库requests和BeautifulSoup来模拟登录微博并获取页面源码,然后使用正则表达式或BeautifulSoup来解析页面,获取评论内容。
以下是一个简单的爬取微博评论的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 登录微博,获取cookies
login_url = 'https://passport.weibo.cn/signin/login'
data = {
'username': 'your_username',
'password': 'your_password',
'savestate': '1',
'entry': 'mweibo',
'mainpageflag': '1',
}
response = requests.post(login_url, data=data)
cookies = response.cookies
# 获取微博页面源码
weibo_url = 'https://m.weibo.cn/api/comments/show?id=123456789' # 将123456789替换为要爬取的微博id
response = requests.get(weibo_url, cookies=cookies)
html = response.text
# 解析页面,获取评论内容
soup = BeautifulSoup(html, 'html.parser')
comments = soup.find_all('div', class_='comment_txt')
for comment in comments:
print(comment.text)
```
注意:在实际使用中,需要注意反爬虫措施,例如设置请求头、延时等。此外,爬取微博评论需要登录后才能获取,需要提前准备好微博账号和密码。
Python爬虫爬取微博用户评论代码
以下是使用Python爬虫爬取微博用户评论的代码,需要先安装requests、beautifulsoup4和lxml库:
```python
import requests
from bs4 import BeautifulSoup
# 获取指定微博的评论页数
def get_comment_pages(weibo_url):
response = requests.get(weibo_url)
soup = BeautifulSoup(response.text, 'lxml')
pages = soup.select('.card-page > ul > li')[-1].get('page')
return int(pages)
# 获取指定微博的所有评论
def get_comments(weibo_url):
pages = get_comment_pages(weibo_url)
comments = []
for page in range(1, pages+1):
url = weibo_url + f'?page={page}'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'lxml')
comment_list = soup.select('.list_li')
for comment in comment_list:
text = comment.select_one('.WB_text').text.strip()
comments.append(text)
return comments
# 测试代码
weibo_url = 'https://weibo.com/1234567890123456/ABCDE6789?type=comment'
comments = get_comments(weibo_url)
for comment in comments:
print(comment)
```
需要替换weibo_url为指定微博的URL。该代码会输出指定微博的所有评论。需要注意的是,微博的评论需要登录后才能查看,所以需要先登录微博才能爬取评论。同时,该代码只能爬取普通用户的评论,无法爬取大V或官方账号的评论。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)