From 26065f0caba01977cec45618326335636395de33 Mon Sep 17 00:00:00 2001 From: Benjamin Mako Hill Date: Mon, 20 Apr 2015 17:15:19 -0700 Subject: [PATCH 1/1] added a version of the final program that writes to a file --- wikipedia4.py => wikipedia4-1.py | 0 wikipedia4-2.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) rename wikipedia4.py => wikipedia4-1.py (100%) create mode 100644 wikipedia4-2.py 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() -- 2.39.5