
The Python requests library simplifies HTTP request tasks such as getting information from websites, posting information, downloading images, following redirects and much more. In this post, we will discuss the basics of the python request library.
Let’s get started!
First, let’s make sure we have the requests library installed. In a command line type:
pip install requests
Now, in a python script import the requests library:
import requests
We will be pulling content from imgur.com, an image sharing and hosting site:

To specify our request, we pass a string containing the url to the ‘get()’ method:
r = requests.get('https://imgur.com/')
To check the response status of our request, we can print the response object:
print(r)

A response code of 200 corresponds to successful request. We can see all of the methods and attributes available to objects in python using the ‘dir()’. Let’s apply ‘dir()’ to our response object:
print(dir(r))

We see that we have things like headers, json, links, ok status, status codes and much more. For a more detailed explanation of object’s attributes and methods, we can apply the ‘help()’ method to our object:
print(help(r))
Let’s look at the details for the ‘text’ attribute:

We can print the text of the response object using the ‘text’ attribute:
print(r.text)

We see the html content for the page we looked at earlier in the browser. We can further parse this html text with tools like ‘BeautifulSoup’. For a good introduction to BeautifulSoup check out Introduction to Web Scraping with BeautifulSoup.
Now, let’s pull an image from the ‘imgur’ site. Let’s navigate to the site:

Next, let’s click on the image of the tree with a power outlet on the lower left:

Right click on the image and click ‘inspect’:

Finally, navigate to the highlighted link, "https://i.imgur.com/1A7VXBR.jpg". You should see that the link directs you to a page with only the image. We can pull this image from ‘imgur’ using requests and the ‘contents’ attribute. Let’s redefine our request object with a link to the image and print the contents:
r = requests.get('https://i.imgur.com/1A7VXBR.jpg')
print(r.content)

If we look back to the help output for our response object, we see that the contents attribute is in bytes:

We can take the bytes from the image and save it to our machine. We will open a file, called ‘tree.png’, in ‘write bytes (wb)’ mode and write the contents to the file:
with open('tree.png', 'wb') as f:
f.write(r.content)
If we open our folder containing the python script, we should see a file called ‘tree.png’ containing the image we selected. I’ll stop here but I encourage you to play around with the code yourself. Feel free to try pulling text and images from other sites using the code in this post.
CONCLUSIONS
To summarize, in this post we discussed the basics of the python requests library. We look at the attributes and methods available to response objects. We used the text attribute to pull the html text for the home page of ‘imgur.’ We also showed how to pull one of the images on the ‘imgur’ site and save it to a file on our machine. I hope you found this post useful/interesting. The code in this post is available on GitHub. Thank you for reading!