python3 获取商店里App评论+解析+存档+筛选
业余爱好抓个接口练练包括创建类、函数、请求接口、数据解析、数据类型转换、函数调用、实现等#!/usr/bin/python#coding=utf8#-*- encoding: UTF-8 -*-# import requests,jsonimport requestsimport json#导入import模块import sys#重新加载sys模块reload(sys)#重新设置字符集sys.s
·
业余爱好
抓个接口练练
开发环境
mac OS 10.15.6
python 3.8.3
包括创建类、函数、请求接口、数据解析、数据类型转换、函数调用、实现等
保存的text在python执行文件的同级目录下,并未做自定义的文件路径,毕竟要的是结果 嘿嘿
#!/usr/bin/python
#coding=utf8
#-*- encoding: UTF-8 -*-
import requests
import json
class AFNet(object):
# 接口中获取feed
def arrFromDic(self,dic):
user_dic = json.loads(dic)
feed = user_dic['feed']
entry = []
if feed.get('entry'):
entry = feed['entry']
return entry
#获取详细信息
def getInfo(self,page,ids):
# 去掉可能包含id
ids = ids.replace("id", "")
url = "https://itunes.apple.com/cn/rss/customerreviews/page=%s/id=%s/sortby=mostrecent/json"%(page, ids)
response = self.get(url);
# print(response)
return response
# get请求
def get(self,url):
print( "请求地址 = "+ url)
response = requests.get(url=url)
return response
# post请求
def post(self,url,parmars):
print(url)
print(parmars)
requests.post(url=url,data={'key1':'value1','key2':'value2'},headers={'Content-Type':'application/x-www-form-urlencoded'})
response = requests.get(url=url)
return response.text
# 获取所有数据
def getAllDate(self,ids):
entry = []
#便历接口
for page in range(1,20):
st = self.getInfo(page,ids)
# st = self.getInfoNew(page,ids)
text = st.text
# print(st)
code = st.status_code
count = len(text)
if code == 200:
entry2 = []
re = self.arrFromDic(text)
if isinstance(re,dict):
entry2.append(re)
else:
entry2 = re
count = len(entry2)
if count <= 0:
print("没有更多数据请求结束")
break
else:
entry = entry + entry2
text = "第 %s 页 %s 条数据"%(page,count)
print (text)
else:
print("请求接口返回异常结束 " + str(code))
break
return entry
# 元数据解析
def managerArr(self,entry):
print ('共 ' + str(len(entry)) + " 条数据")
arrResult= []
for dic in entry:
author = dic["author"]
name = author["name"]
namelabel = name["label"]
content = dic["content"]
contentlabel = content["label"]
title = dic["title"]
titlelabel = title["label"]
version = dic["im:version"]
versionlabel = version["label"]
rating = dic["im:rating"]
ratinglabel = rating["label"]
voteSum = dic["im:voteSum"]
voteSumlabel = voteSum["label"]
start = ""
for x in range(0,int(ratinglabel)):
start = start + "⭐️"
# print (resultSrr)
# arrResult.append(resultSrr)
dicNew = {
"versionlabel":versionlabel,
"start":start,
"ratinglabel":ratinglabel,
"namelabel":namelabel,
"titlelabel":titlelabel,
"contentlabel":contentlabel,
"voteSumlabel":voteSumlabel,
"ratinglabel":ratinglabel
}
arrResult.append(dicNew)
return arrResult
# 字典转字符串 星数
# entry 数组中的字典数据
# strarCount 判断星数
# type 判断条件 1== 2<= 3>=
def managerDicToString(self,entry,strarCount,type):
arrResult= []
for dic in entry:
start = dic["start"]
namelabel = dic["namelabel"]
contentlabel = dic["contentlabel"]
titlelabel = dic["titlelabel"]
versionlabel = dic["versionlabel"]
ratinglabel = dic["ratinglabel"]
voteSumlabel = dic["voteSumlabel"]
start = dic["start"]
resultSrr = ""
resultSrr = resultSrr + "…………………………………………………………………………………………\n"
resultSrr = resultSrr + "版本 = %s\n" %(versionlabel)
resultSrr = resultSrr + "星数 = %s\n" %(start)
resultSrr = resultSrr + "评级 = %s\n" %(ratinglabel)
resultSrr = resultSrr + "昵称 = %s\n" %(namelabel)
resultSrr = resultSrr + "标题 = %s\n" %(titlelabel)
resultSrr = resultSrr + "内容 = %s\n" %(contentlabel)
resultSrr = resultSrr + "投票 = %s\n" %(voteSumlabel)
if type == 1:
if int(strarCount) == int(ratinglabel) :
print("resultSrr = ",resultSrr)
arrResult.append(resultSrr)
pass
pass
if type == 2:
if int(ratinglabel) <= int(strarCount) :
print("resultSrr = ",resultSrr)
arrResult.append(resultSrr)
pass
pass
if type == 3:
if int(ratinglabel) <= int(strarCount) :
print("resultSrr = ",resultSrr)
arrResult.append(resultSrr)
pass
pass
return arrResult
# 保存文件
def SaveFile(self,arr,name):
# print arrResult
count = len(arr)
text = "总数=%s"%(count)
# 文件全名
allName = name + "." + "txt"
try:
f = open(allName,mode='w')
#字典转字符串
str1 = ''.join(saveStrArrNew) #数组转字符串
f.write(str1) # write 写入
f.close() #关闭文件
print("保存成功")
return True
except smtplib.SMTPException:
print("保存失败")
return False
if __name__ == '__main__':
# 请求类的初始化
afn = AFNet()
print("开始")
# app的id 带不带id 都可以 接口有判断逻辑 方便无脑操作 哈哈
ids = '930368978'
ids = 'id930368978'
# str1 = json.dumps(arr)
# 请求分页数据
entry = afn.getAllDate(ids)
saveStrArr = afn.managerArr(entry)
# 字典转字符串 星数
# entry 数组中的字典数据
# strarCount 判断星数
# type 判断条件 1== 2<= 3>=
saveStrArrNew = afn.managerDicToString(saveStrArr,3,2)
# 保存文件
afn.SaveFile(saveStrArrNew,"反馈")
2020-08-25 七夕红豆 中国东方情人节
更新获取评论数量为空的崩溃问题
增加参数id的是否带有id前缀的判断
2020-08-26
更新数据解析
2020-08-27
封装优化
2020-12-15
增加筛选规则
更多推荐
已为社区贡献3条内容
所有评论(0)