h5评论直接显示代码_2019-11-16 爬虫-qq音乐评论

本文介绍了如何爬取QQ音乐周杰伦《说好不哭》的评论,通过分析请求接口,发现评论接口只依赖page和lasthotcommentid参数。通过循环请求这些接口,获取并处理评论数据,最终将评论以H5形式展示。完整代码已上传至GitHub仓库:https://github.com/Liangjianghao/everyDay_spider.gitqqMusic_comments。
摘要由CSDN通过智能技术生成

目标网址:https://y.qq.com/n/yqq/song/001qvvgF38HVc4.html#comment_box

qq音乐 周杰伦的说好不哭

打开charles,刷新网页,复制评论在charles里查找,很容易找到评论接口

e7f264ca22c5109fadefb5604518c022.png
https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg?g_tk=160454710&loginUin=1808163167&hostUin=0&format=json&inCharset=utf8&outCharset=GB2312&notice=0&platform=yqq.json&needNewCode=0&cid=205360772&reqtype=2&biztype=1&topid=237773700&cmd=8&needmusiccrit=0&pagenum=0&pagesize=25&lasthotcommentid=&domain=qq.com&ct=24&cv=10101010

评论翻页,发现只有page,lasthotcommentid参数改变了,page即页数,lasthotcommentid为最后一条评论id

https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg?g_tk=160454710&loginUin=1808163167&hostUin=0&format=json&inCharset=utf8&outCharset=GB2312&notice=0&platform=yqq.json&needNewCode=0&cid=205360772&reqtype=2&biztype=1&topid=237773700&cmd=8&needmusiccrit=0&pagenum=1&pagesize=25&lasthotcommentid=song_237773700_3559701714_1573875409&domain=qq.com&ct=24&cv=10101010

于是直接循环请求:

page=0
lasthotcommentid=''
while 1:

    url='https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg?g_tk=160454710&loginUin=1808163167&hostUin=0&format=json&inCharset=utf8&outCharset=GB2312&notice=0&platform=yqq.json&needNewCode=0&cid=205360772&reqtype=2&biztype=1&topid=237773700&cmd=8&needmusiccrit=0&pagenum=%s&pagesize=25&lasthotcommentid=%s&domain=qq.com&ct=24&cv=10101010'%(page,lasthotcommentid)
    response=requests.get(url,verify=False)
    jsno_data=json.loads(response.text)
    print(jsno_data)
    commentsArr=jsno_data['comment']['commentlist']
    commenttotal=jsno_data['comment']['commenttotal']
    print('共有%s条评论'%commenttotal)
    page+=1
    break

评论格式如图,处理评论并保存

e922bca0e3d93e64bbe746811d5c058f.png
def saveComments(commentsArr):
    for comment in commentsArr:
        nick=comment['nick']
        rootcommentcontent=comment['rootcommentcontent']
        compile=re.compile(r'[em].*[/em].',re.S)
        c=re.sub(compile,'',rootcommentcontent)
        f.write(nick+'----'+c+'n')

结果:

47549e2640a627003ddd74122ad71e3c.png

完整代码:https://github.com/Liangjianghao/everyDay_spider.gitqqMusic_comments

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值