updated wikipedia project to fix windows encoding issue
[wikipedia-api-cdsw] / wikipedia4-2.py
1 import encoding_fix
2 import requests
3
4 # ?action=query&titles=Albert%20Einstein&prop=categories
5 # Get the list of categories for the Albert Einstein article.
6
7 parameters = {'action' : 'query',
8               'titles' : 'Albert Einstein',
9               'prop' : 'categories',
10               'format' : 'json',
11               'continue' :  ''}
12
13 output_file = open("einstein_categories.list", 'w')
14
15 while True:
16     wp_call = requests.get('http://en.wikipedia.org/w/api.php', params=parameters)
17     response = wp_call.json()
18
19     for page_id in response["query"]["pages"].keys():
20         for category in response["query"]["pages"][page_id]['categories']:
21             print(category['title'], file=output_file)
22
23     if 'continue' in response:
24         parameters.update(response['continue'])
25     else:
26         break
27
28
29 output_file.close()

Benjamin Mako Hill || Want to submit a patch?