Различия между двумя страницами при парсинге с помощью Beautiful Soup

Я начинаю с 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)

Моя переменная result ищет строку на определенной странице приложения. Проблема в том, что Google меняет порядок между двумя разными страницами. Иногда result [1] - это имя приложения, иногда - result [2]; Те же проблемы с другими метаданными, которые мне нужны ('обновлено', 'apkSize' и т. Д.). Как я могу справиться с этими изменениями. Можно ли по-другому соскрести? Спасибо


person userHG    schedule 13.11.2018    source источник


Ответы (1)


проблема в том, что цикл python не упорядочен, сохраните его как 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']
person ewwink    schedule 14.11.2018