1

I have a program that looks like this:

import json
import requests
article_name = "BT Centre"
article_api_url = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles={}".format(article_name)
called_data = requests.get(article_api_url)
formatted_data = called_data.json()
print(formatted_data)
pages = formatted_data["query"]["pages"]
print(pages)
first_page = pages[0]["extract"]
print(first_page)

For the first print statement, where it prints the whole JSON, it returns this:

{
  'batchcomplete': '',
  'query':{
    'pages':{
      '18107207':{
        'pageid': 18107207,
        'ns': 0,
        'title':'BT Centre',
        'extract': "The BT Centre is the global headquarters and registered office of BT Group..."
      }
    }
  }
}

When I try to access the "extract" data with the first_page variable, it returns:

Traceback (most recent call last):
  File "wiki_json_printer.py", line 15, in <module>
    first_page = pages[0]["extract"]
KeyError: 0

The problem is, I can't set first_page to pages["18107207"]["extract"] because the Page ID changes for every article.


Edit: Solution from Ann Zen works:

You can use a for loop to loop through the keys of the pages dictionary, and detect which one is the ID via the str.isdigit() method:

for key in pages:
    if key.isdigit():
        print(pages[key]["extract"])
Red
  • 26,798
  • 7
  • 36
  • 58
GameDesert
  • 135
  • 8
  • 1
    Does this answer your question? [How can I get list of values from dict?](https://stackoverflow.com/questions/16228248/how-can-i-get-list-of-values-from-dict) – Håken Lid Feb 19 '21 at 13:54
  • Try `pages.values()[0]["extract"]`. See the suggested duplicate question for more explanation. – Håken Lid Feb 19 '21 at 13:55
  • 1
    Thank you, fortunately, a solution has already been provided, but I will try yours too. Edit: Tested it out, doesn't seem to work, sorry. – GameDesert Feb 19 '21 at 13:58
  • 1
    @HåkenLid The post you linked is not a duplicate of this post. This post wants to get the key that is the ID, with the ID varying each time, while the post you linked only aims to get a list of all the keys. – Red Feb 19 '21 at 13:59

3 Answers3

1

You can use a for loop to loop through the keys of the pages dictionary, and detect which one is the ID via the str.isdigit() method:

for key in pages:
    if key.isdigit():
        print(pages[key]["extract"])
Red
  • 26,798
  • 7
  • 36
  • 58
  • if OP knows there is only ever 1 key `pages[pages.keys()[0]]` and it's just some page id that changes, no loops required – depperm Feb 19 '21 at 13:54
  • @depperm The OP must've reduced the dictionary to provide a minimal code :) – Red Feb 19 '21 at 13:55
1

You could use next on an iterator on the dict to find the first key:

...
key = next(iter(pages))
first_page = pages[key]["extract"]
...
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

pages is dictionary not a list you can't select it by index, use it key

print(pages['18107207']['extract'])

of course the following will work because the key is 18107207

for key in pages:
    print(pages[key]["extract"])
uingtea
  • 6,002
  • 2
  • 26
  • 40