【问题标题】:Differences between two pages while scraping with Beautiful Soup用 Beautiful Soup 抓取时两页之间的差异
【发布时间】:2019-04-16 19:08:14
【问题描述】:

我从 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'等)也有同样的问题我该如何处理这些更改。可以用不同的方式刮吗?谢谢

【问题讨论】:

    标签: python beautifulsoup google-play


    【解决方案1】:

    问题是python循环是not ordered,将其保存为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']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 2016-05-16
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多