模板3之爬取网易云音乐热门评论(post)

import requests
import json
import openpyxl

def get_hot_comments(res):
	comments_json = json.loads(res.text) # json格式转化为python格式
	hot_comments = comments_json['hotComments']
	with open('hot_comments.txt','w',encoding='utf-8') as file:
		for each in hot_comments:
			file.write(each['user']['nickname'] + ':\n\n')
			file.write(each['Content'] + '\n')
			file.write("..............\n")
			
def get_hot_comments_toexcel(res):
	comments_json = json.loads(res.text) # json格式转化为python格式
	hot_comments = comments_json['hotComments']
	wb = openpyxl.Workbook()
	wb.guess_types = True # 允许对单元格数据类型进行猜测
	ws = wb.active
	ws.append(['nickname'],['Content'])
	for each in hot_comments:
		ws.append(each['user']['nickname'],each['Content'] )
		
	ws.save('hot_comments.xlsx')

def get_comments(url):
	name_id = url.split('=')[1] # 用等号分割后取第二个,那首歌的url编号
	headers = {
		'user-agent': 'F12获取',
		'referer': 'F12获取'} # 指定上一个请求从哪来的
	params = "F12获取"
	encSeckey = "F12获取"
	data = {
		"params": params,
		"encSeckey " : encSeckey 
		}
	target_url = "网站对应url编号替换->{}".format(name_id)
	res = requests.post(target_url,headers=headers,data=data)
	return res

def main():
	url = "网址url"
	res = get_commits(url)
	get_hot_commits(res)

main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值