How to get the value "extract" key from Wikipedia JSON response

I want to read the value of the "extract" key in the 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 answer is as follows

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

I deleted the content because it was a lot.

Now the problem is how to programmatically read the page number. The page number changes with different search queries. I definitely don't want to hardcode the page number. What I set instead of page number

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

Is there a way to extract the key from the page tag and then move on to its value?

+4
source share
2

.keys()

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

+1

, Python.keys(). .

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

Source: https://habr.com/ru/post/1673614/


All Articles