Различия между двумя страницами при чистке с Beautiful Soup - PullRequest
0 голосов
/ 14 ноября 2018

Я начинаю с 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» и т. Д.). Как я могу справиться с этими изменениями.Можно ли по-другому скрести?Спасибо

1 Ответ

0 голосов
/ 14 ноября 2018

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