祢豆子oo 发表于 2022-4-6 15:25:19

爬虫代码问题求解

爬https://music.163.com/#/song?id=4466775 中的热门评论,为什么一直报错,求解

代码:

import requests
import json

def get_hot_comments(res):
    comments_json = json.loads(res.text)
    hot_comments = comments_json['hotComments']
    with open('hot_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 get_url(taget_url):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36',
               'referer':'https: // music.163.com /'
            }
    params = 'iT + k0JgfeBhKdvg6o9HwHu8QJZrysl20hnMd3b1UtVmkQDMEHdHsCVlGsfery224wRkqMvXWDRXdpKQ5uezVYCuXWS4wkxhPG0 + kX244E2x3gGt907KHubwcpRnsPBafdvz36dohIvy1 + pIAC99BD4Zu2Xqi + j6 // 2fSFzG6qpvQRcbPtPZaPNV3IFI7aRokAlGTpo9xU6DE0huk6e0I2 + myCVlVDwotMMia91je + vOZjaKsOgSH7eYITpgGgNVvUVakuJ9JG9eBlu9f / 3153w =='
    encSecKey = 'b199f37ec9e03fc43f0d1043803db2a2e4c6beac3a35d6d5b5cdc7bef8bcf9bb4e515cf0d7abf262ed053473a67e9b151b253e81b091a97bea224c3d978358d9282def16494011b95f39b349042458e70d77e1e232edda1ca0c5878a1bc5d197aed08b59d6b8168d9bdb2c8a67133579d8e50ca1b755fbeba3b4f8507e9b9d54'
    data ={
      'params': params,
      'encSecKey': encSecKey
    }

    res = requests.post(taget_url,headers = headers,data = data)
    return res

def main():
    taget_url = 'https://music.163.com/weapi/comment/resource/comments/get?csrf_token='
    res = get_url(taget_url)
    get_hot_comments(res)

if __name__ == '__main__':
    main()

isdkz 发表于 2022-4-6 15:25:20

import requests
import json

def get_hot_comments(res):
    comments_json = json.loads(res.text)
    hot_comments = comments_json['data']['hotComments']                                                         # 改了这里
    with open('hot_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 get_url(taget_url):
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36',
               'referer':'https: // music.163.com /'
            }
    params = 'iT + k0JgfeBhKdvg6o9HwHu8QJZrysl20hnMd3b1UtVmkQDMEHdHsCVlGsfery224wRkqMvXWDRXdpKQ5uezVYCuXWS4wkxhPG0 + kX244E2x3gGt907KHubwcpRnsPBafdvz36dohIvy1 + pIAC99BD4Zu2Xqi + j6 // 2fSFzG6qpvQRcbPtPZaPNV3IFI7aRokAlGTpo9xU6DE0huk6e0I2 + myCVlVDwotMMia91je + vOZjaKsOgSH7eYITpgGgNVvUVakuJ9JG9eBlu9f / 3153w =='
    encSecKey = 'b199f37ec9e03fc43f0d1043803db2a2e4c6beac3a35d6d5b5cdc7bef8bcf9bb4e515cf0d7abf262ed053473a67e9b151b253e81b091a97bea224c3d978358d9282def16494011b95f39b349042458e70d77e1e232edda1ca0c5878a1bc5d197aed08b59d6b8168d9bdb2c8a67133579d8e50ca1b755fbeba3b4f8507e9b9d54'
    data ={
      'params': params,
      'encSecKey': encSecKey
    }

    res = requests.post(taget_url,headers = headers,data = data)
    return res

def main():
    taget_url = 'https://music.163.com/weapi/comment/resource/comments/get?csrf_token='
    res = get_url(taget_url)
    get_hot_comments(res)

if __name__ == '__main__':
    main()

祢豆子oo 发表于 2022-4-6 18:07:13

isdkz 发表于 2022-4-6 15:25


灰常感谢{:10_287:}
页: [1]
查看完整版本: 爬虫代码问题求解