Wikipedia JSON parser in python
Question: i want to print extract of Wikipedia pages but for each search the page no is
changed so how to print extract with wildcard for page no.

i tried following code

    
    
    import urllib2
    import json
    
    response = urllib2.urlopen('https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Stack%20Overflow')
    data = json.load(response)   
    print data["query"]["pages"][0][extract]
    

but it gives error

    
    
    Traceback (most recent call last):
      File "C:/Users/GM/Desktop/pytest/pytest.py", line 6, in <module>
        print data["query"]["pages"][0]["extract"]
    KeyError: 0
    

please help


Answer: Try this:

    
    
    print data["query"]["pages"].values()[0]["extract"]
    

This creates a list of all of the values in the "pages" dictionary. In your
example, there is only one value, so `[0]` gets it.

If there is more than one value, one of them will be returned. It is
unpredictable which one.

