Reputation: 291
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))
Upvotes: 0
Views: 42
Reputation: 2502
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
Upvotes: 1