用靓汤刮两页的区别

2024-04-19 08:05:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我从Python和Beautiful Soup开始,将googleplaystore和应用程序元数据放到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)

我的'result'变量在特定的应用程序页面中查找字符串问题是Google正在更改两个不同页面之间的顺序。有时结果[1]是应用程序名,有时是结果[2];我需要的其他元数据也有同样的问题('updated'、'apkSize'等…)如何处理这些更改。有没有可能用不同的方式刮?谢谢


Tags: 数据textinjsonapp应用程序databs
1条回答
网友
1楼 · 发布于 2024-04-19 08:05:23

问题是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']

相关问题 更多 >