1

I have this API that i'm trying to access and I can't seem to find a way to download all of this data to my computer. Everything i've tried online isnt working, probably because the type of the data is still dict?

Can someone please let me know whether this is the correct way to aggregate all of the data by this website and then how to download it? Thanks

import requests
import string
import json

alphabet = list(string.ascii_lowercase)

fulltable = '{}'

fulltable = json.loads(fulltable)

for letter in alphabet:
    URL = 'https://www.thecocktaildb.com/api/json/v1/1/search.php?f=' + letter
    response = requests.get(URL)
    if response:
        print("Great success! for letter: " + letter)
    else:
        print("OI VEY! for letter: " + letter)

    json_data = response.json()

    print(json_data)
    fulltable.update(json_data)
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • 1
    Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under a [CC BY-SA license](//creativecommons.org/licenses/by-sa/4.0), for SE to distribute the content (i.e. regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any vandalism will be reverted. Please see: [How does deleting work? …](//meta.stackexchange.com/q/5221). If permitted to delete, there's a "delete" button below the post, on the left, but it's only in browsers, not the mobile app. – Makyen Sep 04 '20 at 00:21

1 Answers1

2

You're pretty close to a possible solution. Unfortunately you overwrite your previous result every time you update.

import requests
import string
import json

fulltable = {}
for letter in string.ascii_lowercase:
    url = 'https://www.thecocktaildb.com/api/json/v1/1/search.php?f=' + letter
    response = requests.get(url)
    json_data = response.json()
    for k,v in json_data.items():
        if not k in fulltable:
            fulltable[k] = []
        fulltable[k] += v or []

with open('data.json', 'w') as fp:
    json.dump(fulltable, fp, indent=4, sort_keys=True)
Detlef
  • 6,137
  • 2
  • 6
  • 24
  • ah ok got it, i should have built it sort of like a frequency table updater instead of just appending/updating it. thank you sir! – AbuBakar Kamal Sep 03 '20 at 22:42