From: Benjamin Mako Hill Date: Tue, 21 Apr 2015 00:15:19 +0000 (-0700) Subject: added a version of the final program that writes to a file X-Git-Url: https://projects.mako.cc/source/wikipedia-api-cdsw/commitdiff_plain/26065f0caba01977cec45618326335636395de33?ds=sidebyside added a version of the final program that writes to a file --- diff --git a/wikipedia4.py b/wikipedia4-1.py similarity index 100% rename from wikipedia4.py rename to wikipedia4-1.py diff --git a/wikipedia4-2.py b/wikipedia4-2.py new file mode 100644 index 0000000..11eb967 --- /dev/null +++ b/wikipedia4-2.py @@ -0,0 +1,28 @@ +import requests + +# ?action=query&titles=Albert%20Einstein&prop=categories +# Get the list of categories for the Albert Einstein article. + +parameters = {'action' : 'query', + 'titles' : 'Albert Einstein', + 'prop' : 'categories', + 'format' : 'json', + 'continue' : ''} + +output_file = open("einstein_categories.list", 'w') + +while True: + wp_call = requests.get('http://en.wikipedia.org/w/api.php', params=parameters) + response = wp_call.json() + + for page_id in response["query"]["pages"].keys(): + for category in response["query"]["pages"][page_id]['categories']: + print(category['title'], file=output_file) + + if 'continue' in response: + parameters.update(response['continue']) + else: + break + + +output_file.close()