QQ音乐评论爬虫程序【原创】

先找到一首歌,把请求参数替换到下面程序中

例如:

'g_tk_new_20200303': '5381',

'g_tk':'5381',

'topid':'102636799', //歌曲ID

'cv':'4747474'

...

#此处修改请求的页数

if page >=10:

break

python 复制代码
import requests
from urllib import parse
import json
import pandas as pd
import os
import time


page = 0

df_all = pd.DataFrame()

while True:
	comment_url = "https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg?"
	headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'}
	params = {
	'g_tk_new_20200303': '5381',
	'g_tk':'5381',
	'loginUin':'0',
	'hostUin':'0',
	'format':'json',
	'inCharset':'utf-8',
	'outCharset':'utf-8',
	'notice':'0',
	'platform':'yqq.json',
	'needNewCode':'0',
	'cid':'205360772',
	'reqtype':'1',
	'biztype':'1',
	'topid':'102636799',
	'cmd':'8',
	'needmusiccrit':'0',
	'pagenum':'0',
	'pagesize':'25',
	'lasthotcommentid':'',	
	'domain':'qq.com',
	'ct':'24',
	'cv':'4747474'
	}

	params= parse.urlencode(params)
	url = comment_url + params
	response = requests.get(url,headers=headers)
	result = response.text
	comment_info = json.loads(result)

	topid = comment_info['topid']
	topic_name=comment_info['topic_name']

	comment = comment_info['comment']
	comment_total = comment['commenttotal']
	comment_list = comment['commentlist'] 
	page_total =int((comment_total-1)/25) + 1
	
	#此处修改请求的页数
	if page >=10:
	    break

	df_all = pd.DataFrame()  
	for i in comment_list:
	    
	    comment_id = i['commentid']
	    avatar_url =i['avatarurl']
	    nick=i['nick']
	    try:
	        content=i['rootcommentcontent']
	    except Exception as e:
	        content = ''
	        
	    comment_time=i['time']
	    timeArray  = time.localtime(comment_time)
	    comment_time = time.strftime("%Y年%m月%d日 %H:%M:%S", timeArray)
	    
	    praise_num=i['praisenum']
	    vip_icon=i['vipicon']
	    if vip_icon == '' :
	    	vip_icon = '未开通会员'
	    else:
	        vip_icon = vip_icon[-9:-4]    
	        
	    df = pd.DataFrame({
	        '评论ID':comment_id,
	        '头像链接':avatar_url,
	        '昵称':nick,
	        '评论内容':content,
	        '评论时间':comment_time,
	        '点赞数量':praise_num,
	        '等级图标':vip_icon
	    },index=[0])
	    
	    df_all = pd.concat([df_all,df], ignore_index=True)
	    
	page = page + 1
	time.sleep(1)
	print("第"+str(page)+"页内容获取完毕")
	
df_all.to_excel(os.getcwd()+"\\"+topic_name+'_'+str(comment_total)+'_最新评论.xlsx',index = False)

爬取的数量不对,有些删除的评论也在。

只提供思路,仅用于学习

相关推荐
whoarethenext3 小时前
c/c++爬虫总结
c语言·c++·爬虫
Dreams°1236 小时前
【Python爬虫 !!!!!!政府招投标数据爬虫项目--医疗实例项目文档(提供源码!!!)!!!学会Python爬虫轻松赚外快】
分布式·爬虫·python·mysql·scikit-learn
API小爬虫7 小时前
如何用Jsoup库提取商品名称和价格?
java·爬虫
奋斗者1号1 天前
Docker 部署 - Crawl4AI 文档 (v0.5.x)
人工智能·爬虫·机器学习
Clown951 天前
Go语言爬虫系列教程(一) 爬虫基础入门
开发语言·爬虫·golang
火龙谷2 天前
【爬虫】12306查票
爬虫
ζ小菜鸡2 天前
我用Deepseek + 亮数据爬虫神器 1小时做出輿情分析器
爬虫·bright data
q567315232 天前
Go语言多线程爬虫与代理IP反爬
开发语言·爬虫·tcp/ip·golang
Go Dgg2 天前
Go语言实现豆瓣电影Top250爬虫
开发语言·爬虫·golang
攻城狮7号2 天前
Python爬虫第20节-使用 Selenium 爬取小米商城空调商品
开发语言·数据库·爬虫·python·selenium