python爬虫qq音乐评论
时间: 2023-08-29 09:11:16 浏览: 123
你可以使用Python的requests库和正则表达式来爬取QQ音乐的评论。以下是一个简单的示例代码:
```python
import requests
import re
url = "https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg"
headers = {
"referer": "https://y.qq.com/n/yqq/song/0039MnYb0qxYhV.html",
"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 = {
"g_tk": "5381",
"loginUin": "0",
"hostUin": "0",
"format": "json",
"inCharset": "utf8",
"outCharset": "GB2312",
"notice": "0",
"platform": "yqq.json",
"needNewCode": "0",
"cid": "205360772",
"reqtype": "2",
"biztype": "1",
"topid": "212877816",
"cmd": "8",
"pagenum": "0",
"pagesize": "25",
"lasthotcommentid": "",
"domain": "qq.com",
"ct": "24",
"cv": "10101010"
}
response = requests.get(url, headers=headers, params=params)
comments = re.findall(r'"content":"(.*?)"', response.text)
for comment in comments:
print(comment)
```
在以上代码中,我们首先指定了要爬取的QQ音乐歌曲的评论页链接,然后设置了请求头和请求参数。最后,我们使用requests库发送GET请求,并使用正则表达式提取评论内容。最后,我们打印所有评论。
阅读全文