added a version of the final program that writes to a file
[wikipedia-api-cdsw] / wikipedia4-2.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 output_file = open("einstein_categories.list", 'w')
13
14 while True:
15     wp_call = requests.get('http://en.wikipedia.org/w/api.php', params=parameters)
16     response = wp_call.json()
17
18     for page_id in response["query"]["pages"].keys():
19         for category in response["query"]["pages"][page_id]['categories']:
20             print(category['title'], file=output_file)
21
22     if 'continue' in response:
23         parameters.update(response['continue'])
24     else:
25         break
26
27
28 output_file.close()

Benjamin Mako Hill || Want to submit a patch?