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)

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

只提供思路,仅用于学习

相关推荐
WeeJot嵌入式15 小时前
爬虫对抗:ZLibrary反爬机制实战分析
爬虫·python·网络安全·playwright·反爬机制
进击的雷神16 小时前
攻克JSON嵌套HTML的双重解析难题:基于多层数据提取的精准爬虫设计
爬虫·html·json·spiderflow
前端小趴菜~时倾16 小时前
自我提升-python爬虫学习:day05-函数与面向对象编程
爬虫·python·学习
进击的雷神17 小时前
攻克JSON接口分页与对象数组处理:基于AJAX数据源的精准博客爬虫设计
爬虫·ajax·json·spiderflow
vx_biyesheji000120 小时前
计算机毕业设计:Python汽车数据分析系统 Django框架 requests爬虫 可视化 车辆 数据分析 大数据 机器学习(建议收藏)✅
爬虫·python·算法·机器学习·django·汽车·课程设计
小白学大数据21 小时前
效率翻倍:Scrapy-Redis 分布式全站爬虫并发优化进阶
redis·分布式·爬虫·scrapy
tang777891 天前
小红书平台用什么代理 IP 比较好?2026年3月实测数据 + 选型推荐
网络·爬虫·python·网络协议·tcp/ip·数据挖掘·ip
进击的雷神2 天前
突破POST分页与IP封锁:基于表单提交和代理转发的新闻爬虫设计
爬虫·网络协议·tcp/ip
小邓睡不饱耶2 天前
东方财富股吧话题爬虫实现:从接口请求到Excel数据落地
爬虫·excel
进击的雷神2 天前
攻克动态列表页结构:基于ID与URL双字段协同提取的精准爬虫设计
爬虫·spiderflow