Menu
Coddy logo textTech

requests library

Lesson 3 of 7 in Coddy's Random Fact Generator - Python Project course.

To “fetch” (or get) the content of the API we will use the library called requests.

Using the get method it's possible to get a web page content:

import requests

response = requests.get("https://someurlhere")

The response object has many attributes, the one we will look at is text, which returns a string of the website content:

import requests

response = requests.get("https://someurlhere")
content = response.text
challenge icon

Challenge

Easy

Now, change the function (get_fact) body so that it will get the content from the API end point (from the URL in the previous step) and return the response text!

Don't forget to import requests

Try it yourself

def get_fact():
    return 'https://uselessfacts.jsph.pl/api/v2/facts/random'

All lessons in Random Fact Generator - Python Project