昨天公司產(chǎn)品需要導(dǎo)出appStore上的評論來做產(chǎn)品優(yōu)化,蘋果是有提供這樣的接口的
但是接口返回的是json的格式,也有蠻多不太需要的字段,我用python寫了一個腳本直接獲取并寫成excel的文件 方便使用。直接上代碼
# coding=UTF-8
#!/usr/local/bin/python3.7
import requests
import xlwt
appid ="************" #需要獲取的App的appid
pageNum =10 #獲取最新多少頁的數(shù)據(jù) pageSize為50
cc ='us' #市場地區(qū) US美國 CN中國
output_file ='/Users/user/Documents/result1.xls' #最終文本輸出的文件
def getHTMLText(url):
try:
r = requests.get(url)
return r.json()
except:
return ''
def writeToXls(json,page,sheet):
title = ["用戶", "評分", "標(biāo)題", "內(nèi)容" ,"版本"]
for iin range(len(title)):# 循環(huán)列
sheet.write(0, i, title[i])# 將title數(shù)組中的字段寫入到0行i列中
entry = json["feed"]["entry"]
for infoin entry:
row = entry.index(info) +50*(page-1) +1
sheet.write(row, 0, info["author"]["name"]["label"])
sheet.write(row, 1, info["im:rating"]["label"])
sheet.write(row, 2, info["title"]["label"])
sheet.write(row, 3, info["content"]["label"])
sheet.write(row, 4, info["im:version"]["label"])
def main():
count =0
book = xlwt.Workbook()# 創(chuàng)建一個excel對象
sheet = book.add_sheet('Sheet1', cell_overwrite_ok=True)# 添加一個sheet頁
try:
for iin range(pageNum):
i = i +1
url ='https://itunes.apple.com/rss/customerreviews/page=' +str(i) +'/id=' + appid +'/sortby=mostrecent/json?l=en&&cc=' + cc
json = getHTMLText(url)
writeToXls(json, i, sheet)
count = count +1
print("\r當(dāng)前進(jìn)度: {:.2f}%".format(count *100 /10), end="")
finally:
book.save(output_file)
if __name__ =='__main__':
main()
這幾個是主要的設(shè)置參數(shù),大家打開文件編輯一下然后保存 運行即可惠豺。
appid ="************" #需要獲取的App的appid
pageNum =10 #獲取最新多少頁的數(shù)據(jù) pageSize為50
cc ='us' #市場地區(qū) US美國 CN中國
output_file ='/Users/user/Documents/result1.xls' #最終文本輸出的文件
用到了requests,xlwt兩個庫python 不熟的同學(xué)建議使用Pycharm來下載運行风宁。需要別的信息的可以自行改造writeToXls方法滿足
默認(rèn)是下載最新的500條評論 如果沒有500條洁墙,腳本會報錯,但是數(shù)據(jù)和文件不會有問題戒财。