Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Wikipedia API & Python Library File: wiki.py Until now, we've only worked on our local computers, interacting with local files, but never talking to the

Wikipedia API & Python Library

File: wiki.py

Until now, we've only worked on our local computers, interacting with local files, but never talking to the great big computer in the sky... so let's do that now :)

Many systems have public APIs (Application Programmer Interfaces) that we can use. An API is usually a set of functions that we can call to send and receive data to and from a system. You can write code to interact with things like Twitter, Slack, weather services, government databases, NSA spy satellites and more...

We'll start with Wikipedia.

Instead of writing our own HTTP calls to the Wikipedia API, we can make use of a Python library that abstracts the details away and presents a simpler, Python-based, API for us. We need to install that now.

The lab PCs should let us install packages. If it looks like the package is still installing (infinitely), just carry on as if it were finished, and it should work... or just restart PyCharm if you'd like it to stop telling you it's installing! If you really can't install the wikipedia package, then skip those parts of the prac that use it.

In PyCharm, go to Settings/Preferences > Project: Practicals > Project Interpreter (it might look a bit different, but you should have been here before) and click the plus button to install a package. Type "wikipedia" to find the one we want, and then click "Install Package". Note: If you don't have permission to install this, try it with the option to install to user's site packages directory.

The quick start documentation for the wikipedia package can be found at: https://wikipedia.readthedocs.io/en/latest/quickstart.html

As this documentation shows, a good way to learn a new package like this is via the console. Open the console in PyCharm, then follow the docs and quickly try out functions like search(), summary() and page(). Get a page and see what properties it has.

Create a new file called wiki.py and write a small script that prompts the user for a page title or search phrase, then prints the summary of that page. Use a simple loop that continues doing this until the user enters blank input.

Try this with a few page titles and see what happens. (Note that you might get a warning about an outdated use of the BeautifulSoup package. We can't fix that so ignore it.)

Try it with the search/title "Python", and you should find that the Wikipedia API returns a "disambiguation" page, so you need to handle that exception as explained in the API's docs.

When getting a page, you might find that you get an unexpected result because the API has used suggest() to suggest a particular page, different from what you asked for. You can customise how the page is determined, example:

wikipedia.page(title, autosuggest=False) 

Now modify your program so that when it gets the page, it prints the title, summary and the URL.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Support For Data Mining Applications Discovering Knowledge With Inductive Queries Lnai 2682

Authors: Rosa Meo ,Pier L. Lanzi ,Mika Klemettinen

2004th Edition

3540224793, 978-3540224792

More Books

Students also viewed these Databases questions