Python Requests Library: A Guide

The Python requests library in Python allows you to send HTTP requests easily. The library abstracts much of the complexity of making requests, by using a simple and consistent API. This guide provides an in-depth view of the essential aspects of the requests library and points to further guides that go even deeper.
By the end of this guide, you’ll have learned:
- How to make HTTP requests, including GET and POST requests
- How to customize HTTP requests by using headers and query strings
- How to authenticate requests using different authentication methods
- How to understand the responses you get back, including converting them to Python dictionaries
- How to use proxies for your HTTP requests
- How to create sessions to streamline making multi-part requests
- How to work with timeouts to keep your program running optimally
Table of Contents
What is the Python Requests Library?
The Python requests library is a Python library that handles making HTTP requests. The library is well known for being simple and elegant, by abstracting away much of the complexity of working with HTTP requests.
The library is also one of the more popular libraries available in Python: it currently draws around 30,000,000 downloads per week and is in use by over 1,000,000 repositories on Github.
How Do You Install the Python Requests Library?
The simplest way to install the requests library is to use the Python pip package manager. In order to install the latest version of the library, you can simply call the following command in the command prompt or terminal.
To install a specific version of the library, such as version 2.28.1, you can write the following command:
It’s as easy as that! In the following section, you’ll learn how to install the requests library on macOS using the pip package manager.
Making HTTP GET Requests with the Python Requests Library
An HTTP GET request is used to retrieve data from the specified resource, such as a website. When using the Python requests library, you can use the .get() function to create a GET request for a specified resource.
The .get() function accepts two parameters:
- A url , which points to a resource, and
- params , which accepts a dictionary or tuples to send in the query string
Let’s see how we can use the get() function to make a GET request:
Let’s break down what we did in the code above:
- We imported the requests library
- We created a new variable, resp , which is the result of passing our endpoint into the get() function
- We then printed out the variable, which returned a Response object
The Response object has a number of different properties and methods that we can access to learn more about the data that has been returned.
Some of the key properties and methods available to a Response object include:
- .status_code , which is the integer code of the responded HTTP status, such as 200 or 404
- .ok , which return True is the status code of the response is less than 400, otherwise False
- .json() , which returns the JSON-encoded content of a response
- .content , which contains the content of the response
- .next , which returns a request for the new request in a redirect chain, if there is one
In the following section, you’ll learn how to make POST requests using the Python requests library.
Making HTTP POST Requests with the Python Requests Library
An HTTP POST request is used to send data to a server, where data are shared via the body of a request. In the request.post() function, data are sent with the data parameter, which accepts a dictionary, a list of tuples, bytes or a file object.
Let’s take a look at what the requests.post() function looks like in Python:
We can see that the function accepts three parameters:
- url , which defines the URL to post to
- data , which accepts a dictionary, list of tuples, butes, or a file-like object to send in the body of the request
- json , which represents json data to send in the body of the request
To illustrate using this function, we’ll be using the https://httpbin.org/ website, which allows us to place actual POST requests.
Let’s create our first POST request using Python:
In the example above, we simply pass the URL to the /post endpoint into the requests.post() function. We then print the response that we get back, which returns a Response object.
Customizing Headers in HTTP Requests Using Python Requests
HTTP headers allow the client and server to pass additional information while sending an HTTP request or receiving a subsequent response. These headers are case-insensitive, meaning that the header of ‘User-Agent’ could also be written as ‘user-agent’ .
Additionally, HTTP headers in the requests library are passed in using Python dictionaries, where the key represents the header name and the value represents the header value.
HTTP headers allow the client and server to pass additional information while sending an HTTP request or receiving a subsequent response. These headers are case-insensitive, meaning that the header of ‘User-Agent’ could also be written as ‘user-agent’ .
Additionally, HTTP headers in the requests library are passed in using Python dictionaries, where the key represents the header name and the value represents the header value.
Customizing Query String in HTTP Requests Using Python Requests
Query string parameters allow you to customize a GET request by passing values directly into the URL or into the params= parameter.
There are three ways to accomplish creating query parameters:
- Pass in a dictionary of parameters into the params= parameter, such as
- Pass in a list of tuples into the params= parameter, such as [(‘page’, ‘2’)]
- Directly modify the URL string by appending ?page=2 to the URL
Let’s see how we can use the first method to accomplish this:
Even when you don’t pass in headers, headers will be returned in a Response object. The headers are accessible via the .headers attribute, which returns a dictionary.
Let’s see how we can return the headers available in a Response object:
In the code block above, we used a get() function call to get a Response object. We then accessed the headers of the response by accessing the .headers attribute.
How to Understand Responses from HTTP Requests in Python Requests
The Python requests library returns a Response object when any type of request is made, including POST and GET requests. The Response object contains useful attributes and methods that help in understand the response.
Before diving further, let’s see how we can create a Response object and verify its type:
We can see that when we make a request, in this case a GET request, a Response object is returned.
Understanding whether or not a request was successful is, of course, a very important part of understanding a response. The requests.Response object provides a number of different ways to do this. Let’s explore two key attributes:
- Response.status_code returns an integer value representing the status of the response
- Response.ok returns a boolean value indicating success
A common task you’ll want to take on is seeing the body of a response that gets returned when a request is made. There are a number of simple ways in which we can do this:
- .content returns the actual content in bytes
- .text returns the content converted to a string, using a character encoding such as UTF-8
Both of these methods work well for accessing the content of the response. Using the .text attribute simply converts the values to a string for easier access.
How to Convert Python Requests Responses to JSON
The requests library comes with a helpful method, .json(), that helps serialize the response of the request. In order to look at an example, let’s the public endpoints provided by the website reqres.in . These endpoints work without signing up, so following along with the tutorial is easy.
Let’s see how we can access the /users endpoint and serialize the response into a Python dictionary using the .json() method:
From the code above, we can see that applying the .json() method to our response created a Python dictionary.
How to Authenticate HTTP Requests with Python Requests
The Python requests library provides a number of different ways to authenticate requests, which are covered in detail in this post. Let’s first take a look at basic authentication.
Basic authentication refers to using a username and password for authentication a request. Generally, this is done by using the HTTPBasicAuth class provided by the requests library. However, as you’ll later learn, the requests library makes this much easier, as well, by using the auth= parameter.
Let’s see how we can pass in a username and password into a simple GET request using the HTTPBasicAuth class:
Let’s break down what we did in the code above:
- We imported both the requests library as well as only the HTTPBasicAuth class
- We then created a new HTTPBasicAuth object, auth , which contains a string for the username and password
- Finally, we printed the Response we returned when passing our auth variable into the auth= parameter of the get() function
If you were using this method, you’d change ‘user’ and ‘pass’ to the username and password of your choice.
Because the basic authentication method is used so frequently, the requests library abstracts away some of this complexity. Rather than needing to create a new HTTPBasicAuth object each time, you can simply pass a tuple containing your username and password into the auth= parameter.
Let’s see what this looks like:
In the code above, we were able to significantly reduce the complexity of our code. The Python requests library handles a lot of the boilerplate code for us!
To learn more about other forms of authentication, check out this dedicated article on authenticating HTTP requests with Python.
How to Create Sessions with Python Requests
A Session object in the Python requests library allows you to persist parameters across different requests being made. Similarly, it allows you to persist cookies across requests. Finally, it also used connection pooling, which can make subsequent requests gain significant performance improvements.
What’s great about requests Session objects is that they allow you to persist items, such as authentication, across multiple requests. This allows you to, for example, scrape websites much more elegantly than you may otherwise.
An important thing to note is that a session object has access to all the methods of the main Requests API.
You can create a session object by instantiating a Session class, as shown below:
In the following section, you’ll learn how to set headers for a Python requests Session.
For example, you can persist in authenticating requests using Python Session objects. This allows you to pass in authentication to the Session object. This can be done by setting the .auth attribute of the session directly.
Let’s see how this can be done using Python:
In the example above, we created a Session object. Then, we assigned a tuple containing a username and password to the .auth attribute of the Session object.
How to Set Timeouts for HTTP Requests with Python Requests
By default, the Python requests library does not set a timeout for any request it sends. This is true for GET , POST , and PUT requests. While this can prevent unexpected errors, it can result in your request running indefinitely.
Because of this, it’s important to set a timeout to prevent unexpected behavior.
In order to set a timeout in an HTTP request made via the requests library, you can use the timeout parameter. The parameter accepts either an integer or a floating point value, which describes the time in seconds.
It’s important to note that this behavior is different from many other HTTP request libraries, such as those in JavaScript. In other libraries or languages, this behavior tends to be expressed in milliseconds.
Let’s take a look at an example of how we can send a GET request with a timeout:
In the example above, we set a timeout of 3 seconds. We used an integer to represent the time of our timeout. If we wanted to be more precise, we could also pass in a floating point value:
By passing in a single value, we set a timeout for the request. If we wanted to set different timeouts for connecting and reading a request, we can pass in a tuple of values.
How to Use Proxies When Using Python Requests
In order to use proxies in the requests Python library, you need to create a dictionary that defines the HTTP, HTTPS, and FTP connections. This allows each connection to map to an individual URL and port. This process is the same for any request being made, including GET requests and POST requests.
It’s important to note that while the connections map to individual URLs and ports, they can actually point to the same URL and port.
Let’s see how you can define a set of proxies for the Python requests library:
Let’s break down what we did above:
- We imported the requests library
- We defined a dictionary, proxy_servers , which mapped URLs and ports to HTTP and HTTPS connections
- We then made a GET request and passed in our dictionary into the proxies= argument
Conclusion
In this guide, you learned how to use the Python requests library to make HTTP requests. The library provides a lot of functionality to streamline and simplify making HTTP requests and working with their responses.
First, you learned how to make HTTP requests, including GET and POST requests. Then, you learned how to customize these requests using headers and query strings. You also learned how to authenticate requests in various ways.
Following this, you learned how to understand the responses that get returned. You also learned how to use sessions to make requests simpler and persistent. Then, you also learned how to use proxies and timeouts to manage the requests that you make.
Python’s Requests Library (Guide)
The requests library is the de facto standard for making HTTP requests in Python. It abstracts the complexities of making requests behind a beautiful, simple API so that you can focus on interacting with services and consuming data in your application.
Throughout this article, you’ll see some of the most useful features that requests has to offer as well as how to customize and optimize those features for different situations you may come across. You’ll also learn how to use requests in an efficient way as well as how to prevent requests to external services from slowing down your application.
In this tutorial, you’ll learn how to:
- Make requests using the most common HTTP methods
- Customize your requests’ headers and data, using the query string and message body
- Inspect data from your requests and responses
- Make authenticated requests
- Configure your requests to help prevent your application from backing up or slowing down
Though I’ve tried to include as much information as you need to understand the features and examples included in this article, I do assume a very basic general knowledge of HTTP. That said, you still may be able to follow along fine anyway.
Now that that is out of the way, let’s dive in and see how you can use requests in your application!
Take the Quiz: Test your knowledge with our interactive “HTTP Requests With the "requests" Library” quiz. Upon completion you will receive a score so you can track your learning progress over time:
Getting Started With requests
Let’s begin by installing the requests library. To do so, run the following command:
If you prefer to use Pipenv for managing Python packages, you can run the following:
Once requests is installed, you can use it in your application. Importing requests looks like this:
Now that you’re all set up, it’s time to begin your journey through requests . Your first goal will be learning how to make a GET request.
The GET Request
HTTP methods such as GET and POST , determine which action you’re trying to perform when making an HTTP request. Besides GET and POST , there are several other common methods that you’ll use later in this tutorial.
One of the most common HTTP methods is GET . The GET method indicates that you’re trying to get or retrieve data from a specified resource. To make a GET request, invoke requests.get() .
To test this out, you can make a GET request to GitHub’s Root REST API by calling get() with the following URL:
Congratulations! You’ve made your first request. Let’s dive a little deeper into the response of that request.
The Response
A Response is a powerful object for inspecting the results of the request. Let’s make that same request again, but this time store the return value in a variable so that you can get a closer look at its attributes and behaviors:
In this example, you’ve captured the return value of get() , which is an instance of Response , and stored it in a variable called response . You can now use response to see a lot of information about the results of your GET request.
Status Codes
The first bit of information that you can gather from Response is the status code. A status code informs you of the status of the request.
For example, a 200 OK status means that your request was successful, whereas a 404 NOT FOUND status means that the resource you were looking for was not found. There are many other possible status codes as well to give you specific insights into what happened with your request.
By accessing .status_code , you can see the status code that the server returned:
.status_code returned a 200 , which means your request was successful and the server responded with the data you were requesting.
Sometimes, you might want to use this information to make decisions in your code:
With this logic, if the server returns a 200 status code, your program will print Success! . If the result is a 404 , your program will print Not Found .
requests goes one step further in simplifying this process for you. If you use a Response instance in a conditional expression, it will evaluate to True if the status code was between 200 and 400 , and False otherwise.
Therefore, you can simplify the last example by rewriting the if statement:
Technical Detail: This Truth Value Test is made possible because __bool__() is an overloaded method on Response .
This means that the default behavior of Response has been redefined to take the status code into account when determining the truth value of the object.
Keep in mind that this method is not verifying that the status code is equal to 200 . The reason for this is that other status codes within the 200 to 400 range, such as 204 NO CONTENT and 304 NOT MODIFIED , are also considered successful in the sense that they provide some workable response.
For example, the 204 tells you that the response was successful, but there’s no content to return in the message body.
So, make sure you use this convenient shorthand only if you want to know if the request was generally successful and then, if necessary, handle the response appropriately based on the status code.
Let’s say you don’t want to check the response’s status code in an if statement. Instead, you want to raise an exception if the request was unsuccessful. You can do this using .raise_for_status() :
If you invoke .raise_for_status() , an HTTPError will be raised for certain status codes. If the status code indicates a successful request, the program will proceed without that exception being raised.
Further Reading: If you’re not familiar with Python 3.6’s f-strings, I encourage you to take advantage of them as they are a great way to simplify your formatted strings.
Now, you know a lot about how to deal with the status code of the response you got back from the server. However, when you make a GET request, you rarely only care about the status code of the response. Usually, you want to see more. Next, you’ll see how to view the actual data that the server sent back in the body of the response.
Content
The response of a GET request often has some valuable information, known as a payload, in the message body. Using the attributes and methods of Response , you can view the payload in a variety of different formats.
To see the response’s content in bytes , you use .content :
While .content gives you access to the raw bytes of the response payload, you will often want to convert them into a string using a character encoding such as UTF-8. response will do that for you when you access .text :
Because the decoding of bytes to a str requires an encoding scheme, requests will try to guess the encoding based on the response’s headers if you do not specify one. You can provide an explicit encoding by setting .encoding before accessing .text :
If you take a look at the response, you’ll see that it is actually serialized JSON content. To get a dictionary, you could take the str you retrieved from .text and deserialize it using json.loads() . However, a simpler way to accomplish this task is to use .json() :
The type of the return value of .json() is a dictionary, so you can access values in the object by key.
You can do a lot with status codes and message bodies. But, if you need more information, like metadata about the response itself, you’ll need to look at the response’s headers.
Headers
The response headers can give you useful information, such as the content type of the response payload and a time limit on how long to cache the response. To view these headers, access .headers :
.headers returns a dictionary-like object, allowing you to access header values by key. For example, to see the content type of the response payload, you can access Content-Type :
There is something special about this dictionary-like headers object, though. The HTTP spec defines headers to be case-insensitive, which means we are able to access these headers without worrying about their capitalization:
Whether you use the key ‘content-type’ or ‘Content-Type’ , you’ll get the same value.
Now, you’ve learned the basics about Response . You’ve seen its most useful attributes and methods in action. Let’s take a step back and see how your responses change when you customize your GET requests.
Query String Parameters
One common way to customize a GET request is to pass values through query string parameters in the URL. To do this using get() , you pass data to params . For example, you can use GitHub’s Search API to look for the requests library:
By passing the dictionary <'q': 'requests+language:python'>to the params parameter of .get() , you are able to modify the results that come back from the Search API.
You can pass params to get() in the form of a dictionary, as you have just done, or as a list of tuples:
You can even pass the values as bytes :
Query strings are useful for parameterizing GET requests. You can also customize your requests by adding or modifying the headers you send.
Request Headers
To customize headers, you pass a dictionary of HTTP headers to get() using the headers parameter. For example, you can change your previous search request to highlight matching search terms in the results by specifying the text-match media type in the Accept header:
The Accept header tells the server what content types your application can handle. In this case, since you’re expecting the matching search terms to be highlighted, you’re using the header value application/vnd.github.v3.text-match+json , which is a proprietary GitHub Accept header where the content is a special JSON format.
Before you learn more ways to customize requests, let’s broaden the horizon by exploring other HTTP methods.
Other HTTP Methods
Aside from GET , other popular HTTP methods include POST , PUT , DELETE , HEAD , PATCH , and OPTIONS . requests provides a method, with a similar signature to get() , for each of these HTTP methods:
Each function call makes a request to the httpbin service using the corresponding HTTP method. For each method, you can inspect their responses in the same way you did before:
Headers, response bodies, status codes, and more are returned in the Response for each method. Next you’ll take a closer look at the POST , PUT , and PATCH methods and learn how they differ from the other request types.
The Message Body
According to the HTTP specification, POST , PUT , and the less common PATCH requests pass their data through the message body rather than through parameters in the query string. Using requests , you’ll pass the payload to the corresponding function’s data parameter.
data takes a dictionary, a list of tuples, bytes, or a file-like object. You’ll want to adapt the data you send in the body of your request to the specific needs of the service you’re interacting with.
For example, if your request’s content type is application/x-www-form-urlencoded , you can send the form data as a dictionary:
You can also send that same data as a list of tuples:
If, however, you need to send JSON data, you can use the json parameter. When you pass JSON data via json , requests will serialize your data and add the correct Content-Type header for you.
httpbin.org is a great resource created by the author of requests , Kenneth Reitz. It’s a service that accepts test requests and responds with data about the requests. For instance, you can use it to inspect a basic POST request:
You can see from the response that the server received your request data and headers as you sent them. requests also provides this information to you in the form of a PreparedRequest .
Inspecting Your Request
When you make a request, the requests library prepares the request before actually sending it to the destination server. Request preparation includes things like validating headers and serializing JSON content.
You can view the PreparedRequest by accessing .request :
Inspecting the PreparedRequest gives you access to all kinds of information about the request being made such as payload, URL, headers, authentication, and more.
So far, you’ve made a lot of different kinds of requests, but they’ve all had one thing in common: they’re unauthenticated requests to public APIs. Many services you may come across will want you to authenticate in some way.
Authentication
Authentication helps a service understand who you are. Typically, you provide your credentials to a server by passing data through the Authorization header or a custom header defined by the service. All the request functions you’ve seen to this point provide a parameter called auth , which allows you to pass your credentials.
One example of an API that requires authentication is GitHub’s Authenticated User API. This endpoint provides information about the authenticated user’s profile. To make a request to the Authenticated User API, you can pass your GitHub username and password in a tuple to get() :
The request succeeded if the credentials you passed in the tuple to auth are valid. If you try to make this request with no credentials, you’ll see that the status code is 401 Unauthorized :
When you pass your username and password in a tuple to the auth parameter, requests is applying the credentials using HTTP’s Basic access authentication scheme under the hood.
Therefore, you could make the same request by passing explicit Basic authentication credentials using HTTPBasicAuth :
Though you don’t need to be explicit for Basic authentication, you may want to authenticate using another method. requests provides other methods of authentication out of the box such as HTTPDigestAuth and HTTPProxyAuth .
You can even supply your own authentication mechanism. To do so, you must first create a subclass of AuthBase . Then, you implement __call__() :
Here, your custom TokenAuth mechanism receives a token, then includes that token in the X-TokenAuth header of your request.
Bad authentication mechanisms can lead to security vulnerabilities, so unless a service requires a custom authentication mechanism for some reason, you’ll always want to use a tried-and-true auth scheme like Basic or OAuth.
While you’re thinking about security, let’s consider dealing with SSL Certificates using requests .
SSL Certificate Verification
Any time the data you are trying to send or receive is sensitive, security is important. The way that you communicate with secure sites over HTTP is by establishing an encrypted connection using SSL, which means that verifying the target server’s SSL Certificate is critical.
The good news is that requests does this for you by default. However, there are some cases where you might want to change this behavior.
If you want to disable SSL Certificate verification, you pass False to the verify parameter of the request function:
requests even warns you when you’re making an insecure request to help you keep your data safe!
Note: requests uses a package called certifi to provide Certificate Authorities. This lets requests know which authorities it can trust. Therefore, you should update certifi frequently to keep your connections as secure as possible.
Performance
When using requests , especially in a production application environment, it’s important to consider performance implications. Features like timeout control, sessions, and retry limits can help you keep your application running smoothly.
Timeouts
When you make an inline request to an external service, your system will need to wait upon the response before moving on. If your application waits too long for that response, requests to your service could back up, your user experience could suffer, or your background jobs could hang.
By default, requests will wait indefinitely on the response, so you should almost always specify a timeout duration to prevent these things from happening. To set the request’s timeout, use the timeout parameter. timeout can be an integer or float representing the number of seconds to wait on a response before timing out:
In the first request, the request will timeout after 1 second. In the second request, the request will timeout after 3.05 seconds.
You can also pass a tuple to timeout with the first element being a connect timeout (the time it allows for the client to establish a connection to the server), and the second being a read timeout (the time it will wait on a response once your client has established a connection):
If the request establishes a connection within 2 seconds and receives data within 5 seconds of the connection being established, then the response will be returned as it was before. If the request times out, then the function will raise a Timeout exception:
Your program can catch the Timeout exception and respond accordingly.
The Session Object
Until now, you’ve been dealing with high level requests APIs such as get() and post() . These functions are abstractions of what’s going on when you make your requests. They hide implementation details such as how connections are managed so that you don’t have to worry about them.
Underneath those abstractions is a class called Session . If you need to fine-tune your control over how requests are being made or improve the performance of your requests, you may need to use a Session instance directly.
Sessions are used to persist parameters across requests. For example, if you want to use the same authentication across multiple requests, you could use a session:
Each time you make a request with session , once it has been initialized with authentication credentials, the credentials will be persisted.
The primary performance optimization of sessions comes in the form of persistent connections. When your app makes a connection to a server using a Session , it keeps that connection around in a connection pool. When your app wants to connect to the same server again, it will reuse a connection from the pool rather than establishing a new one.
Max Retries
When a request fails, you may want your application to retry the same request. However, requests will not do this for you by default. To apply this functionality, you need to implement a custom Transport Adapter.
Transport Adapters let you define a set of configurations per service you’re interacting with. For example, let’s say you want all requests to https://api.github.com to retry three times before finally raising a ConnectionError . You would build a Transport Adapter, set its max_retries parameter, and mount it to an existing Session :
When you mount the HTTPAdapter , github_adapter , to session , session will adhere to its configuration for each request to https://api.github.com.
Timeouts, Transport Adapters, and sessions are for keeping your code efficient and your application resilient.
Conclusion
You’ve come a long way in learning about Python’s powerful requests library.
You’re now able to:
- Make requests using a variety of different HTTP methods such as GET , POST , and PUT
- Customize your requests by modifying headers, authentication, query strings, and message bodies
- Inspect the data you send to the server and the data the server sends back to you
- Work with SSL Certificate verification
- Use requests effectively using max_retries , timeout , Sessions, and Transport Adapters
Because you learned how to use requests , you’re equipped to explore the wide world of web services and build awesome applications using the fascinating data they provide.
Take the Quiz: Test your knowledge with our interactive “HTTP Requests With the "requests" Library” quiz. Upon completion you will receive a score so you can track your learning progress over time:
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Making HTTP Requests With Python
Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

About Alex Ronquillo
Alex Ronquillo is a Software Engineer at thelab. He’s an avid Pythonista who is also passionate about writing and game development.
Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:


Master Real-World Python Skills With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
Master Real-World Python Skills
With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
What Do You Think?
What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.
Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal. Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!
Response 200 python что значит

Python request module is a simple and elegant Python HTTP library. It provides methods for accessing Web resources via HTTP. In the following article, we will use the HTTP GET method in the Request module. This method requests data from the server and the Exception handling comes in handy when the response is not successful. Here, we will go through such situations. We will use Python’s try and except functionality to explore the exceptions that arise from the Requests module.
- url: Returns the URL of the response
- raise_for_status(): If an error occur, this method returns a HTTPError object
- request: Returns the request object that requested this response
- status_code: Returns a number that indicates the status (200 is OK, 404 is Not Found)
Successful Connection Request
The first thing to know is that the response code is 200 if the request is successful.
Знакомство с Python библиотекой requests

Подробное руководство по Python библиотеке requests с примерами использования.
Введение
Большая часть интернет-ресурсов взаимодействует с помощью HTTP-запросов. Эти HTTP-запросы выполняются устройствами или браузерами (клиентами) при обращении к веб-сервису.
В Python библиотека requests позволяет делать HTTP-запросы в нашем коде, и это очень востребовано, так как многие современные приложения используют данные сторонних сервисов с помощью API.
Python requests — отличная библиотека. Она позволяет выполнять GET и POST запросы с возможностью передачи параметров URL, добавления заголовков, размещения данных в форме и многое другое.
С помощью библиотеки можно делать запросы практически на каждый сайт/веб-страницу, но ее сила заключается в доступе к API для получения данных в виде JSON, с которыми можно работать в своем коде, приложениях и скриптах.
Установка библиотеки
Чтобы установить библиотеку requests, запустите команду:
Будет установлен модуль и зависимости, если таковые имеются.
Создание простого запроса
Давайте попробуем сделать простой запрос на мой сайт, https://egorovegor.ru (вы можете использовать любой сайт):
Для начала импортируем модуль requests. Из него мы используем функцию get() с переданным URL нашего сайта. В этой строке кода делается запрос на https://egorovegor.ru, а ответ сохраняется в переменную r.
Для выполнения запроса к удаленному ресурсу требуется подключение к сети интернет.
Теперь у нас есть объект ответа (response), присвоенный переменной r. Мы можем получить всю необходимую информацию из этого объекта.
После того, как запрос сделан, мы получаем ответ от веб-сервера на котором расположен сайт и можем прочитать его код:
Если ваш код состояния равен 200, это означает что запрос был выполнен успешно. С полным списком кодов состояния HTTP можно ознакомится на странице в Википедии. Вы можете получить доступ к исходному коду веб-страницы с помощью свойства .text:
Весь исходный код веб-страницы будет распечатан в вашей консоли.
Это полезно при выполнении сбора данных с веб страниц.
Выполнение запросов к API
Максимально раскрыться библиотеке позволяет взаимодействие с внешними API. В данном руководстве мы будем использовать API, который доступен на движке моего сайте. Мы выполним запрос, который должен вернуть информацию о сайте.
Теперь у нас есть объект response, сохраненный в переменную r. Мы можем получить из него всю необходимую информацию.
Содержимое ответа
Мы можем читать содержимое различными способами, используя атрибуты и функции, предоставляемые модулем requests.
r.status_code возвращает код, указывающий, был ли запрос успешным или нет. 200 означает успешный. Общие коды статусов, которые вы, вероятно, видели — 200, 404 и 500. 404 означает ошибку клиента, а 500 означает ошибку сервера.
r.encoding возвращает кодировку ответа, основанную на HTTP заголовках.
r.url возвращает запрошенный URL.
r.json возвращает разобранные JSON данные из ответа.
r.text возвращает ответ в текстовом формате
r.content возвращает ответ, отформатированный в байтах
Работа с JSON ответами
JSON-данные, полученные по ссылке https://egorovegor.ru/wp-json/, содержат много информации о сайте, давайте попробуем с ней поработать.
Ответ сервера:

Обратите внимание, я сократил ответ сервера из за большого количества информации в json.
r.json() разбирает ответ в Python-совместимый тип данных, т.е. словарь или список. Разберем на примере как использовать полученные JSON данные.
Выполнив данный сценарий мы получим результат указанный ниже.
Использование параметров в URL
При работе с запросами можно передавать параметры в строке запроса URL. Параметры URL — распространенный способ передачи данных, их часто можно увидеть после вопросительного знака в URL. Пример: https://egorovegor.ru/?s=Python — это URL для поиска статей с ключевым словом Python, получается что s=Python — это его параметр.
Чтобы передать параметры в URL, их нужно указать как словарь в аргумент ключевого слова params. Например; ?s=Python будет записан как <‘s’:’Python’>.
Наверняка вы заметили, что в r.url сохранился URL, который автоматически добавил к нему параметр; ?s=Python.
Заключение
Мы рассмотрели как использовать библиотеку requests в Python и научились парсить данные с сайтов.
Для более глубокого изучения библиотеки requests можно обратиться к официальной документации.