我从Python和Beautiful Soup开始,我将Google PlayStore和应用程序元数据抓取到JSON文件中。这是我的代码:
def createjson(app_link):
url = 'https://play.google.com/store/apps/details?id=' + app_link
response = get(url)
html_soup = BeautifulSoup(response.text, 'html.parser')
bs = BeautifulSoup(response.text,"lxml")
result = [e.text for e in bs.find_all("div",{"class":"hAyfc"})]
apptype = [e.text for e in bs.find_all("div",{"class":"hrTbp R8zArc"})]
data = {}
data['appdata'] = []
data['appdata'].append({
'name': html_soup.find(class_="AHFaub").text,
'updated': result[1][7:],
'apkSize': result[2][4:],
'offeredBy': result[9][10:],
'currentVersion': result[4][15:]
})
jsonfile = "allappsdata.json" #Get all the appS infos in one JSON
with open(jsonfile, 'a+') as outfile:
json.dump(data, outfile)
我的“结果”变量在特定应用页面中查找字符串,问题是Google更改了两个不同页面之间的顺序。有时result [1]是应用程序名称,有时是result [2];我需要的其他元数据也存在同样的问题(“更新”,“ apkSize”等)。如何处理这些更改。可以用其他方式刮擦吗?谢谢
答案 0 :(得分:1)
问题是python循环为not ordered,将其另存为dict not list。使用
更改您的result = [e....]
result = {}
details = bs.find_all("div",{"class":"hAyfc"})
for item in details:
label = item.findChild('div', {'class' : 'BgcNfc'})
value = item.findChild('span', {'class' : 'htlgb'})
result[label.text] = value.text
还有data['appdata']...
和
data['appdata'].append({
'name': html_soup.find(class_="AHFaub").text,
'updated': result['Updated'],
'apkSize': result['Size'],
'offeredBy': result['Offered By'],
'currentVersion': result['Current Version']