python3 获取商店里App评论+解析+存档+筛选
生活随笔
收集整理的這篇文章主要介紹了
python3 获取商店里App评论+解析+存档+筛选
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
業余愛好
抓個接口練練
開發環境
mac OS 10.15.6
python 3.8.3
包括創建類、函數、請求接口、數據解析、數據類型轉換、函數調用、實現等
保存的text在python執行文件的同級目錄下,并未做自定義的文件路徑,畢竟要的是結果 嘿嘿
#!/usr/bin/python #coding=utf8 #-*- encoding: UTF-8 -*- import requests import jsonclass AFNet(object):# 接口中獲取feeddef 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):# 去掉可能包含idids = 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_codecount = len(text)if code == 200:entry2 = []re = self.arrFromDic(text)if isinstance(re,dict):entry2.append(re)else:entry2 = recount = len(entry2)if count <= 0:print("沒有更多數據請求結束")breakelse:entry = entry + entry2text = "第 %s 頁 %s 條數據"%(page,count)print (text)else:print("請求接口返回異常結束 " + str(code))breakreturn 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)passpassif type == 2:if int(ratinglabel) <= int(strarCount) :print("resultSrr = ",resultSrr)arrResult.append(resultSrr)passpassif type == 3:if int(ratinglabel) <= int(strarCount) :print("resultSrr = ",resultSrr)arrResult.append(resultSrr)passpassreturn arrResult# 保存文件def SaveFile(self,arr,name):# print arrResultcount = 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 Trueexcept smtplib.SMTPException:print("保存失敗")return Falseif __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
增加篩選規則
?
總結
以上是生活随笔為你收集整理的python3 获取商店里App评论+解析+存档+筛选的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android开发之获取SIM卡信息和手
- 下一篇: 护盾的实现