Как авторизоваться на сайте через python requests
Authentication refers to giving a user permissions to access a particular resource. Since, everyone can’t be allowed to access data from every URL, one would require authentication primarily. To achieve this authentication, typically one provides authentication data through Authorization header or a custom header defined by server.
Example –
Replace “user” and “pass” with your username and password. It will authenticate the request and return a response 200 or else it will return error 403.
If you an invalid username or password, it will return an error as –
Types of Authentication
Digest Authentication
Another very popular form of HTTP Authentication is Digest Authentication, and Requests supports this out of the box as well:
OAuth 1 Authentication
A common form of authentication for several web APIs is OAuth. The requests-oauthlib library allows Requests users to easily make OAuth 1 authenticated requests:
For more information on how to OAuth flow works, please see the official OAuth website. For examples and documentation on requests-oauthlib, please see the requests_oauthlib repository on GitHub
OAuth 2 and OpenID Connect Authentication
The requests-oauthlib library also handles OAuth 2, the authentication mechanism underpinning OpenID Connect. See the requests-oauthlib OAuth2 documentation for details of the various OAuth 2 credential management flows:
Other Authentication
Requests is designed to allow other forms of authentication to be easily and quickly plugged in. Members of the open-source community frequently write authentication handlers for more complicated or less commonly-used forms of authentication. Some of the best have been brought together under the Requests organization, including:
If you want to use any of these forms of authentication, go straight to their GitHub page and follow the instructions.
Authentication¶
This document discusses using various kinds of authentication with Requests.
Many web services require authentication, and there are many different types. Below, we outline various forms of authentication available in Requests, from the simple to the complex.
Basic Authentication¶
Many web services that require authentication accept HTTP Basic Auth. This is the simplest kind, and Requests supports it straight out of the box.
Making requests with HTTP Basic Auth is very simple:
In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand for using it:
Providing the credentials in a tuple like this is exactly the same as the HTTPBasicAuth example above.
netrc Authentication¶
If no authentication method is given with the auth argument, Requests will attempt to get the authentication credentials for the URL’s hostname from the user’s netrc file. The netrc file overrides raw HTTP authentication headers set with headers= .
If credentials for the hostname are found, the request is sent with HTTP Basic Auth.
Digest Authentication¶
Another very popular form of HTTP Authentication is Digest Authentication, and Requests supports this out of the box as well:
OAuth 1 Authentication¶
A common form of authentication for several web APIs is OAuth. The requests-oauthlib library allows Requests users to easily make OAuth 1 authenticated requests:
For more information on how to OAuth flow works, please see the official OAuth website. For examples and documentation on requests-oauthlib, please see the requests_oauthlib repository on GitHub
OAuth 2 and OpenID Connect Authentication¶
The requests-oauthlib library also handles OAuth 2, the authentication mechanism underpinning OpenID Connect. See the requests-oauthlib OAuth2 documentation for details of the various OAuth 2 credential management flows:
Other Authentication¶
Requests is designed to allow other forms of authentication to be easily and quickly plugged in. Members of the open-source community frequently write authentication handlers for more complicated or less commonly-used forms of authentication. Some of the best have been brought together under the Requests organization, including:
If you want to use any of these forms of authentication, go straight to their GitHub page and follow the instructions.
New Forms of Authentication¶
If you can’t find a good implementation of the form of authentication you want, you can implement it yourself. Requests makes it easy to add your own forms of authentication.
To do so, subclass AuthBase and implement the __call__() method:
When an authentication handler is attached to a request, it is called during request setup. The __call__ method must therefore do whatever is required to make the authentication work. Some forms of authentication will additionally add hooks to provide further functionality.
Further examples can be found under the Requests organization and in the auth.py file.
Requests is an elegant and simple HTTP library for Python, built for human beings. You are currently looking at the documentation of the development release.
Авторизация на сайте через питон
Я описал как парсить сайты через модуль requests. Бывают случаи когда для части сайта нужна авторизация, то есть логин и пароль.
Для авторизации нужно поддерживать активную сессию в модуле requests, через session.get()

В оригинальной статье было снимание куков, я не совсем понимаю когда использовать. В данном скрипе, я это не использую.
Дальше посылаем post-запрос
Для того чтобы узнать пост запрос, нужно зайти в инспектор кода, вкладка сеть, и найти запрос который посылается

Теперь переходим в закрытый раздел, обновляем сессию и снимаем данные

Дальше можно снимать нужную информацию, используя BeautifulSoup
Для библиотеки Requests, есть хорошие статьи от Александра, pythonru
Продвинутое руководство по библиотеке Python Requests — дополнительная информация, по объектам session, сертификатам SSL, прокси.
How to "log in" to a website using Python's Requests module?
I am trying to post a request to log in to a website using the Requests module in Python but its not really working. I’m new to this. so I can’t figure out if I should make my Username and Password cookies or some type of HTTP authorization thing I found (??).
So now, I think I’m supposed to use «post» and cookies.
I have a feeling that I’m doing the cookies thing wrong. I don’t know.
If it doesn’t log in correctly, the title of the home page should come out to «Locationary.com» and if it does, it should be «Home Page.»
If you could maybe explain a few things about requests and cookies to me and help me out with this, I would greatly appreciate it. 😀
. It still didn’t really work yet. Okay. so this is what the home page HTML says before you log in:
So I think I’m doing it right, but the output is still «Locationary.com»
I want to be able to stay logged in for a long time and whenever I request a page under that domain, I want the content to show up as if I were logged in.
6 Answers 6
I know you’ve found another solution, but for those like me who find this question, looking for the same thing, it can be achieved with requests as follows:
Firstly, as Marcus did, check the source of the login form to get three pieces of information — the url that the form posts to, and the name attributes of the username and password fields. In his example, they are inUserName and inUserPass.
Once you’ve got that, you can use a requests.Session() instance to make a post request to the login url with your login details as a payload. Making requests from a session instance is essentially the same as using requests normally, it simply adds persistence, allowing you to store and use cookies etc.
Assuming your login attempt was successful, you can simply use the session instance to make further requests to the site. The cookie that identifies you will be used to authorise the requests.