added a version of the final program that writes to a file
[wikipedia-api-cdsw] / wikipedia4-1.py
1 import requests
2
3 # ?action=query&titles=Albert%20Einstein&prop=categories
4 # Get the list of categories for the Albert Einstein article.
5
6 parameters = {'action' : 'query',
7               'titles' : 'Albert Einstein',
8               'prop' : 'categories',
9               'format' : 'json',
10               'continue' :  ''}
11
12 while True:
13     wp_call = requests.get('http://en.wikipedia.org/w/api.php', params=parameters)
14     response = wp_call.json()
15
16     for page_id in response["query"]["pages"].keys():
17         for category in response["query"]["pages"][page_id]['categories']:
18             print(category['title'])
19
20     if 'continue' in response:
21         parameters.update(response['continue'])
22     else:
23         break
24

Benjamin Mako Hill || Want to submit a patch?