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()
模板3之爬取网易云音乐热门评论(post)
最新推荐文章于 2021-01-30 20:54:55 发布