import requests
from bs4 import BeautifulSoup
import json
import xlwt
#存储成Excel表格的函数
def save_excel(comment,save_file):
#第一个参数的整个评论存储所在的列表
wb=xlwt.Workbook()
sheet=wb.add_sheet('评论')
for i in range(len(comment)): #第i个评论列表
for j in range(len(comment[i])):
sheet.write(i,j,comment[i][j])
wb.save('C:/Users/Xpeng/Desktop/爬取到的表格/'+save_file)
#----爬取QQ音乐某首歌(topid区分)的评论
def song_comment(topid):
url='https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg'
comment_id=''
all_com=[]
for p in range(10):
params={
'g_tk':'5381',
'loginUin':'943413047',
'hostUin': '0',
'format': 'json',
'inCharset': 'utf8',
'outCharset':'GB2312',
'notice':'0',
'platform':'yqq.json',
'needNewCode':'0',
'cid':'205360772',
'reqtype':'2',
'biztype':'1',
'topid':topid, #这个是歌曲的id,识别标志,在Headers的参数中获取
'cmd':'8',
'needmusiccrit':'0',
'pagenum':str(p), #第p+1页的评论,QQ音乐是从0计数的
'pagesize':'25',
'lasthotcommentid':comment_id,