腾讯课堂学院评论抓取(jsonpath)

# Author: Sooele
import requests
import jsonpath
####jsonpath

url='https://ke.qq.com/cgi-bin/comment_new/course_comment_list?cid=231516&count=10&page=0&filter_rating=0&bkn=1127518385&r=0.30652732073484557'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36',
    'cookie':  '***************************',  
########自行提取账号cookie
    'referer':'https://ke.qq.com/course/231516?tuin=3551c1eb',
}

response = requests.get(url,headers=headers).json()
name_list=jsonpath.jsonpath(response,'$..nick_name')
#######'$..nick_name'    $代表result   .代表层级
# print(name_list)
###
comment_list=jsonpath.jsonpath(response,'$..first_comment')
# print(name_list,comment_list)

# print(response)
for i in range(len(name_list)):
    print(name_list[i],comment_list[i]) #输出评论

    with open('pinglun.txt','a',encoding='utf-8')as f:
        f.write(name_list[i]+'\n')
        f.write(comment_list[i]+'\n')