python爬虫爬取微博评论
时间: 2023-08-28 19:07:32 浏览: 94
要爬取微博评论,可以使用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)
```
注意:在实际使用中,需要注意反爬虫措施,例如设置请求头、延时等。此外,爬取微博评论需要登录后才能获取,需要提前准备好微博账号和密码。