0

I'm beginning with Python and Beautiful Soup and I'm scraping Google PlayStore and applications metadata in a JSON file. Here is my code :

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)

My 'result' variable looks for a string in a specific app page the problem is that Google is changing the order between two different pages. Sometimes result[1] is the application name, sometimes it's result[2]; Same problems for other metadata I need ('updated', 'apkSize', etc...) How can I deal with these changes. Is it possible to scrape in a different way? Thank you

userHG
  • 567
  • 4
  • 29

1 Answers1

1

the problem is python loop is not ordered, save it as dict not list. Change your result = [e....] with

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

also data['appdata']... with

data['appdata'].append({
    'name': html_soup.find(class_="AHFaub").text,
    'updated': result['Updated'],
    'apkSize': result['Size'],
    'offeredBy': result['Offered By'],
    'currentVersion': result['Current Version']
ewwink
  • 18,382
  • 2
  • 44
  • 54