|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 小小鹏 于 2020-9-2 17:32 编辑
- import requests
- import json
- import bs4
- import re
-
- def get_comments(url):
- headers = {
- 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36',
- 'referer': 'http://music.163.com/'
- }
- params = "AUC/rfnG4oOUAf0ug1rfO7V34dxLIaUuQ+19BGlGvSwcoIO/2k65YhyQJayprwPFeJx2iGjBHVB527JFlzQ6R6hy2GMNqqd214vFrLXZo8YYfYdcbm4+e/M+XoGKAU/KEgjhwue0IVf7dvcNPcEezDuvNs8632tNbLIdM33TFdcTtlrTM+waZ/jI0609s+ilDrkmnrMhtf7x2sUx3TuHurbPfTkbkzwVWo0mJDKexCvWrA1Znl7FZ7D0mNWB6yi6WJPpkYAthWj9IYgizpGlTcLG8IEbxW6Bahe2B10AIY0="
- encSecKey = "2e8f3c153486e277b289fabe631805f60960e851317fa7559aa0a1db756b8ac018c67cd94370deec8e7b4923093126d1f9caa6fce1aac2d49656b856830bcaafebd6684bd2ace730378fe71ba4ad0283faf89e73284aaf36130531b58972f0d5ed584c58c98d71df9c2247e3510b7db23335e87154d620d981cc8e8d8636ccc5"
- data = {
- "params": params,
- "encSecKey": encSecKey
- }
- target_url = "https://music.163.com/weapi/comment/resource/comments/get?csrf_token="
- res = requests.post(target_url, headers=headers, data=data)
- return res
- def find_depth(res):
- soup = bs4.BeautifulSoup(res.text, 'html.parser')
- depth = soup.find('a', class_='zbtn znxt js-n-1599034418092')
- return int(depth)
- def get_all_comments(res):
- comments_json = json.loads(res.text)
- all_comments = comments_json['data']['comments']
- with open('all_comments.txt', 'w', encoding='utf-8') as file:
- for each in hot_comments:
- file.write(each['user']['nickname'] + ':\n\n')
- file.write(each['content'] + '\n')
- file.write("---------------------------------------\n")
-
- def main():
- url = input("请输入链接地址:")
- res = get_comments(url)
- depth = find_depth(res)
- result = []
- for i in range(depth):
- url = "https://music.163.com/#/song?id=1450574147"
- res = get_comments(url)
- result.extend(res)
-
- get_all_comments(res)
- if __name__ == "__main__":
- main()
复制代码
运行就报错了,请问是什么原因吗?
本帖最后由 1q23w31 于 2020-9-2 21:23 编辑
第一个问题:每一首歌曲的encSecKey和params是不同的,随便输入一个链接,网易云肯定会检测到你是爬虫,因为你发送的歌曲链接和评论地址对不上,
第二个问题:每一首歌曲中评论的class属性是不同的,你用一个不变的属性,来匹配随意一个歌曲的网页,结果肯定为空
第三个问题:你让输入的歌曲链接,貌似没有用到
你让输入的链接,到底是啥链接
|
|