以下是爬取某音评论区的 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'}
# 模拟登录并获取cookies
session = requests.session()
login_url = 'https://passport.xiaomi.com/oauth2/login'
session.get(login_url, headers=headers)
login_api = 'https://account.xiaomi.com/pass/serviceLoginAuth2?_json=true'
post_data = {
'sid': 'passport.xiaomi.com',
'callback': 'https://www.xiaomiyoupin.com/callback',
'qs': '%3FredirectUrl%3Dhttps%253A%252F%252Fm.xiaomiyoupin.com%252Fcomment%252Flist%253FgoodsId%253D118280%2526pageIndex%253D1%2526pageSize%253D10',
'_sign': 'yR4M%2Fq%2BMFbqzFYjxZyJ3n9yMx7M%3D',
'serviceParam': '{"checkSafePhone":false}',
'service': 'mipay',
'loginTime': '1629805765',
'showExtra': 'true',
'callbackUrl': 'https://www.xiaomiyoupin.com/callback',
'qsStamp': 'eyJpdiI6InRnWU9pV1B0NzJQcHRoV3h1Zll1TlE9PSIsInZhbHVlIjoiWnZPRjN0bGY1QWpCQng5UWtWc1p4Zz09IiwibWFjIjoiYTJhMzg1ZjdhZjIwNmYzYzk0ZGJmMDY1ZjhhODM3YjQ0MjBhNjQ3YzhlZjM2NzczZjhiZmE5OGJjYjczMjBjZSJ9',
'sidTimeout': '1h',
'_json': 'true',
'qsToken': 'eyJpdiI6IjJiNTdUZ21haE1aZzZmQ0xlQXh4dEE9PSIsInZhbHVlIjoiYStGXC9WQzBtRmZ1NU1Wb0tqT1ZUZz09IiwibWFjIjoiYzY2NTk0MjRlNDg0ZmY0ODRmNzU1ZGEyNTUzNzAzNzIzOWUyMzgyMjYzYjlkNjQ2ZDQ1MmRjZjNiNTQ2ZjEyZCJ9',
'deviceId': 'c7b4f0e9-b9da-31da-9c77-9b15a6b64a14',
'pwdMethod': 'pwd',
'user': 'your_account', # 替换成你的账号
'hash': 'your_password' # 替换成你的密码
}
session.post(login_api, headers=headers, data=post_data)
# 获取评论列表
comment_url = 'https://m.xiaomiyoupin.com/comment/list?goodsId=118280&pageIndex=1&pageSize=10'
response = session.get(comment_url, headers=headers)
if response.status_code == 200:
result = json.loads(response.text)
comments = result['data']['list']
for comment in comments:
print(comment['content'])
else:
print('获取评论失败')
```