准备
1.python 3.7
2.需要安装的库:
requests
jsonpath
pandas
time
fake_useragent
4.由于链接提供的json数据键值对不好观察,需要一个json格式转换工具
json转换工具
转换前
转换后
转换后
代码
import requests
import jsonpath
import pandas as pd
import time
from fake_useragent import UserAgent
ua = UserAgent()
headers = {'User-Agent': ua.random}
#时间转换,原始时间例如"time":1497320494166,转换成例如'2017-06-13 10:21:34.166'年月日时分秒
def stampToTime(stamp):
datatime = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(float(str(stamp)[0:10])))
datatime = datatime+'.'+str(stamp)[10:]
return datatime
#获取json数据
def get_json(url):
try:
response = requests.get(url, headers=headers)
if response.status_code == 200:
json_text = response.json()
return json_text
except Exception:
print('此页有问题!')
return None
#获取评论时间,用户id,用户昵称,评论
def get_comments(url):
data = []
doc = get_json(url)#调用get_json()函数,获取json数据
jobs = doc['hotComments']
for job in jobs:
dic = {}
#使用jsonpath获取需要提取的键值对数据,jsonpath相关内容见官网地址
#官网地址 https://pypi.org/project/jsonpath-ng/
#调用stampToTime()函数转换时间
dic['time'] = stampToTime(jsonpath.jsonpath(job, '$..time')[0])#时间
dic['userId'] = jsonpath.jsonpath(job['user'], '$..userId')[0] # 用户ID
dic['nickname'] = jsonpath.jsonpath(job['user'], '$..nickname')[0] # 用户名
dic['content'] = jsonpath.jsonpath(job, '$..content')[0].replace('\r', '')#评论
data.append(dic)
return data
def main():
#评论信息用json数据表示,url时json数据的网址
url="http://music.163.com/api/v1/resource/comments/R_SO_4_483671599?limit=10&offset=0"
comments=get_comments(url)
#comments的类型时list,每个元素都是一个字典,将其每个元素改成list,方便写入csv
# print(type(comments))
commentslist=[]
for single_comment in comments:
singleuser = []
singleuser.append(single_comment['time'])
singleuser.append(single_comment['userId'])
singleuser.append(single_comment['nickname'])
singleuser.append(single_comment['content'])
commentslist.append(singleuser)
# print(commentslist)
columnsName=['时间','用户ID','用户名','评论']
#list没有to_csv的属性,也就是说list直接是转存不了为csv,
# 为了解决这个问题,我们可以引入panas模块,使用其DataFrame属性。
testdata=pd.DataFrame(columns=columnsName,data=commentslist)
#将列表testdata存为csv文件
testdata.to_csv('comments.csv',encoding='gbk')
if __name__ == "__main__":
main()