added a version of the final program that writes to a file
[wikipedia-api-cdsw] / wikipedia4-1.py
diff --git a/wikipedia4-1.py b/wikipedia4-1.py
new file mode 100644 (file)
index 0000000..1798d1a
--- /dev/null
@@ -0,0 +1,24 @@
+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' :  ''}
+
+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'])
+
+    if 'continue' in response:
+        parameters.update(response['continue'])
+    else:
+        break
+

Benjamin Mako Hill || Want to submit a patch?