# Author:Nimo_Ding
'''
目标:
获取七里香歌曲的所有最新评论。
先了解什么是"带参数请求数据":
Request URL:'https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg?g_tk_new_20200303=5381&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=GB2312¬ice=0&platform=yqq.json&needNewCode=0&cid=205360772&reqtype=2&biztype=1&topid=102065756&cmd=8&needmusiccrit=0&pagenum=0&pagesize=25&lasthotcommentid=&domain=qq.com&ct=24&cv=10101010'
Query String Parameters:(查询字符串参数,这些就是url链接请求中所附带的参数。)
g_tk_new_20200303: 5381
g_tk: 5381
loginUin: 0
hostUin: 0
format: json
inCharset: utf8
outCharset: GB2312
notice: 0
platform: yqq.json
needNewCode: 0
cid: 205360772
reqtype: 1
biztype: 1
topid: 102065756
cmd: 4
needmusiccrit: 0
pagenum: 0
pagesize: 0
lasthotcommentid:
domain: qq.com
# 通过对比第一页第二页评论,可以发现只有pagenum这个参数不一样。
'''
# 这个url是请求歌曲评论的url参数的前面部分
url = 'https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg'
import requests
f=open('05歌曲评论.html','w',encoding='utf-8')
for i in range(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': '102065756',
'cmd': '6',
'needmusiccrit': '0',
'pagenum': str(i), # 每页不同的就是这个页码
'pagesize': '15',
'lasthotcommentid': 'song_102065756_3202544866_44059185',
'domain': 'qq.com',
'ct': '24',
'cv': '10101010'
}
json_comments=requests.get(url,params=params).json()
comment=json_comments['comment']['commentlist']
for j in comment:
print('--->')
print(j['rootcommentcontent'])
f.write(j['rootcommentcontent'])
f.write('\n\n')
f.close()