Rajeev Desai
Rajeev Desai

Reputation: 144

How to get value of "extract" key from a Wikipedia JSON response

I want read the value of "extract" key in a Wikipedia JSON response in Python3. The URL I'm testing with is https://en.wikipedia.org/w/api.php?action=query&titles=San%20Francisco&prop=extracts&format=json.

The response looks like this

{
  "batchcomplete": "",
  "query": {
    "pages": {
      "49728": {
        "pageid": 49728,
        "ns": 0,
        "title": "San Francisco",
        "extract": "<p><b>San Francisco</b></p>"
      }
    }
  }
}

I removed the content as it was a lot.

Now the problem is how do I read the page number programmatically. The page number changes with different searches. I definitely don't want to hard code the page number. What do I put instead of page number

content = response.query.pages.<page number>.extract

Is there any way to extract the key from the pages tag and then proceed to get it's value?

Upvotes: 3

Views: 164

Answers (2)

Will Elson
Will Elson

Reputation: 341

One possible way to do this using the .keys() method

page_number = list(json["query"]["pages"].keys())[0]

Upvotes: 1

Rajeev Desai
Rajeev Desai

Reputation: 144

I found that I can solve this problem by using Python's .keys() method. I did this.

key = list(response['query']['pages'].keys())
print(response['query']['pages'][key[0]]['extract'])

Upvotes: 1

Related Questions