Checking out the Wikipedia API
You're doing so well and having so much fun that we're going to throw one more API at you: the Wikipedia API (documented here). You'll figure out how to find and extract information from the Wikipedia page for Pizza. What gets a bit wild here is that your query will return nested JSONs, that is, JSONs with JSONs, but Python can handle that because it will translate them into dictionaries within dictionaries.
The URL that requests the relevant query from the Wikipedia API is
https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=pizza
Questo esercizio fa parte del corso
Intermediate Importing Data in Python
Istruzioni dell'esercizio
- Assign the relevant URL to the variable
url. - Apply the
json()method to the response objectrand store the resulting dictionary in the variablejson_data. - The variable
pizza_extractholds the HTML of an extract from Wikipedia's Pizza page as a string; use the functionprint()to print this string to the shell.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Import package
import requests
# Assign URL to variable: url
# Always include a descriptive User-Agent (Wikipedia requires this)
headers = {
"User-Agent": "Checking out the Wikipedia API"
}
# Package the request, send the request and catch the response: r
r = requests.get(url, headers=headers)
# Decode the JSON data into a dictionary: json_data
# Print the Wikipedia page extract
pizza_extract = json_data['query']['pages']['24768']['extract']
____