Created
April 18, 2023 15:27
-
-
Save macloo/a55a2cc66d24ca207ade2f0e6cafe345 to your computer and use it in GitHub Desktop.
Access the Wikipedia API with Python, Flask, and Requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Flask example using Requests library syntax to build | |
a Wikipedia API query - no templates, just raw HTML | |
written straight into the browser. | |
This code was partly written by ChatGPT. | |
""" | |
from flask import Flask, jsonify, request, redirect, url_for | |
import random, requests | |
app = Flask(__name__) | |
# list of random topics | |
mylist = ["David Bowie", "Marie Curie", "foobar", "Blade Runner", "Sherlock Holmes", "API", "Vespa", "St. Augustine, Florida", "arrabbiata"] | |
@app.route('/', methods=['GET', 'POST']) | |
def index(): | |
# filling the form input and Return/Enter will submit a POST request | |
if request.method == 'POST': | |
page_title = request.form['userinput'] | |
return redirect( url_for('get_main_points', page_title=page_title) ) | |
# or a GET request writes this to the browser | |
pagename = random.choice(mylist) | |
html = "<h1>Wikipedia API calls</h1><ol>" | |
html += "<li><a href='/get_main_points/" + pagename + "'>Click to see " | |
html += pagename | |
html += " info</a><br><br></li><li><form method='POST'>" | |
html += "Or you can type any name: " | |
html += "<input type='text' id='userinput' name='userinput'></form>" | |
html += "</li></ol>" | |
return html | |
@app.route('/get_main_points/<string:page_title>', methods=['GET']) | |
def get_main_points(page_title): | |
# set up API request | |
endpoint = 'https://en.wikipedia.org/w/api.php' | |
params = { | |
'action': 'query', | |
'format': 'json', | |
'titles': page_title, | |
'prop': 'extracts', | |
'explaintext': True, | |
'exsectionformat': 'wiki', | |
'redirects': True, | |
'exsentences': 3 # get first 3 sentences of the page | |
} | |
# make API request - using Requests library | |
response = requests.get(endpoint, params=params).json() | |
# extract main points from API response | |
page_id = list(response['query']['pages'].keys())[0] | |
page_title = response['query']['pages'][page_id]['title'] | |
page_extract = response['query']['pages'][page_id]['extract'] | |
# return main points as JSON response | |
# return jsonify({ | |
# 'page_title': page_title, | |
# 'page_extract': page_extract | |
# }) | |
heading = '<h1>' + page_title + '</h1>' | |
text = '<p>' + page_extract + '</p>' | |
link = '<p><a href="' + url_for('index') + '">Return to index</a></p>' | |
return heading + text + link | |
if __name__ == '__main__': | |
# app.run(debug=True) | |
app.run(host='0.0.0.0', port=4999, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for this code!