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)

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

只提供思路,仅用于学习

相关推荐
电商API_180079052473 小时前
微店常用API:获取商品详情接口|关键字搜索商品接口|获取快递费接口-打通商品运营与用户体验的技术桥梁
大数据·服务器·人工智能·爬虫·数据挖掘
绒绒毛毛雨21 小时前
爬虫前奏--基于macos的ip代理池构建
爬虫·tcp/ip·macos
B站_计算机毕业设计之家21 小时前
基于大数据的短视频数据分析系统 Spark哔哩哔哩视频数据分析可视化系统 Hadoop大数据技术 情感分析 舆情分析 爬虫 推荐系统 协同过滤推荐算法 ✅
大数据·hadoop·爬虫·spark·音视频·短视频·1024程序员节
一晌小贪欢1 天前
Python爬虫第10课:分布式爬虫架构与Scrapy-Redis
分布式·爬虫·python·网络爬虫·python爬虫·python3
疏狂难除1 天前
关于spiderdemo第二题的奇思妙想
javascript·爬虫
麦麦大数据2 天前
D030知识图谱科研文献论文推荐系统vue+django+Neo4j的知识图谱|论文本文相似度推荐|协同过滤
vue.js·爬虫·django·知识图谱·科研·论文文献·相似度推荐
Serendipity_Carl2 天前
爬虫数据清洗可视化案例之全球灾害数据
爬虫·python·pycharm·数据可视化·数据清洗
B站_计算机毕业设计之家3 天前
spark实战:python股票数据分析可视化系统 Flask框架 金融数据分析 Echarts可视化 大数据技术 ✅
大数据·爬虫·python·金融·数据分析·spark·股票
深蓝电商API3 天前
反爬升级:WAF、行为检测、指纹追踪,我们该如何应对?
爬虫·waf·反爬
疏狂难除3 天前
spiderdemo第四题
爬虫·okhttp·webassembly