]> projects.mako.cc - wikipedia-api-cdsw/commitdiff
added a version of the final program that writes to a file
authorBenjamin Mako Hill <mako@atdot.cc>
Tue, 21 Apr 2015 00:15:19 +0000 (17:15 -0700)
committerBenjamin Mako Hill <mako@atdot.cc>
Tue, 21 Apr 2015 00:15:19 +0000 (17:15 -0700)
wikipedia4-1.py [moved from wikipedia4.py with 100% similarity]
wikipedia4-2.py [new file with mode: 0644]

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 (file)
index 0000000..11eb967
--- /dev/null
@@ -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()

Benjamin Mako Hill || Want to submit a patch?