首页 > 解决方案 > 用 Beautiful Soup 抓取时两页之间的差异

问题描述

我从 Python 和 Beautiful Soup 开始,我在 JSON 文件中抓取 Google PlayStore 和应用程序元数据。这是我的代码:

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];我需要的其他元数据('updated'、'apkSize'等)也有同样的问题我该如何处理这些更改。可以用其他方式刮吗?谢谢

标签: pythonbeautifulsoupgoogle-play

解决方案


问题是python循环没有排序,将其保存为dict而不是列表。改变你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']

推荐阅读