0

I'd like to access the value of extract key that is nested in the pages key

{
    "batchcomplete": "",
    "query": {
        "normalized": [
            {
                "from": "sample",
                "to": "Sample"
            }
        ],
        "pages": {
            "23895873": {
                "pageid": 23895873,
                "ns": 0,
                "title": "Sample",
                "extract": "<p><b>Sample</b> or <b>samples</b> may refer to:</p>\n<p></p>\n"
            }
        }
    }
}

I am creating a wikipedia bot that will print the summary (value of the key "extract") . But the problem is that the "pageid" value keeps on changing with the search result . How can I do this?

I tried using json:

import json
import requests
wikiReq = requests.get("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro=&titles=sample&format=json")
jsonResult = wikiReq.json()
result = jsonResult["query"]["pages"][""]["extract"]
print(json.dumps(result , indent = 4))

1 Answer 1

1

You can do

for i in jsonResult["query"]["pages"]:
    result = jsonResult["query"]["pages"][i]["extract"]

Assuming there is just one item in there it will always work

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.