Login to website in Python

flow

  1. Go to the login page of the service you want to log in to and check the login form parameters
  2. Send the required information to the login URL and start the session

In the following, we will use Qiita as an example.

How to check the parameters

The source of the Qiita login form is as follows:

<form class="landingLoginForm" autocomplete="off" data-event_name="Login with password" action="/login" accept-charset="UTF-8" method="post">
  <input name="utf8" type="hidden" value="✓">
  <input type="hidden" name="authenticity_token" value="rYIMTVoDlb4eCzh6wZIRgiPQHmrr5ts9DyykDCE9FkHM7zQxX7WAhmUhW8y0BPIA3MvzH31KCFEyiPTVk4GBzQ==">
  <input type="text" name="identity" id="identity" placeholder="Username or email" autofocus="autofocus" class="form-control landingLoginForm_identity">
  <div class="row">
    <div class="col-sm-9 landingLoginForm_passwordColumn">
      <input type="password" name="password" id="password" placeholder="Password" class="form-control">
    </div>
    <div class="col-sm-3 landingLoginForm_submitColumn">
      <input type="submit" name="commit" value="Login" class="btn btn-primary btn-block" data-disable-with="Login">
    </div>
  </div>
  <div class="landingLoginForm_forgotPassword">
  	<a href="https://qiita.com/sessions/forgot_password">Forgot Password?</a>
  </div>
  <div class="help-block js-email-invalid-message" style="display: none"></div>
</form>

これより、ログインボタンを押すとqiita.com/login

  1. utf8
  2. authenticity_token
  3. identity
  4. password

It can be seen that the data is transmitted. Of these, utf8 is given a default value, so use it, and authenticity_token is given a value for each access, so you need to get it. Enter the information by hand in the remaining identity and `` `password```.

Communication with the site

Python has a convenient library for HTTP communication called requests, so it's safe to use it. After logging in, it is necessary to save the value for session, so communicate via the `` `Session``` object.

Also, as mentioned above, it is necessary to obtain authenticity_token before logging in, so first access the top page and BeautifulSoup. Get the value with /).

Below is a sample script to log in to Qiita:

from bs4 import BeautifulSoup
import requests

payload = {
    'utf8': '✓',
    'identity': 'username or email',
    'password': 'secret'
}

# authenticity_Get token
s = requests.Session()
r = s.get('https://qiita.com')
soup = BeautifulSoup(r.text)
auth_token = soup.find(attrs={'name': 'authenticity_token'}).get('value')
payload['authenticity_token'] = auth_token

#Login
s.post('https://qiita.com/login', data=payload)

#Perform the processing that is possible after logging in below
...

Caution

In some cases, the above simple method does not work (for example, two-step authentication is required to log in to Google), so it is necessary to check the HTTP header information etc. in detail each time. ..

Recommended Posts

Login to website in Python
To flush stdout in Python
Speech to speech in python [text to speech]
How to develop in Python
Post to Slack in Python
[Python] How to do PCA in Python
Convert markdown to PDF in Python
How to collect images in Python
How to use SQLite in Python
In the python command python points to python3.8
Try to calculate Trace in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
How to handle Japanese in Python
An alternative to `pause` in Python
Quadtree in Python --2
Python in optimization
I tried to implement PLSA in Python
Metaprogramming in Python
Updated to Python 2.7.9
[Introduction to Python] How to use class in Python?
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Try logging in to qiita with Python
Install Pyaudio to play wave in python
How to access environment variables in Python
I tried to implement permutation in Python
Method to build Python environment in Xcode 6
Meta-analysis in Python
How to dynamically define variables in Python
How to do R chartr () in Python
Unittest in python
Pin current directory to script directory in Python
[Itertools.permutations] How to put permutations in Python
PUT gzip directly to S3 in Python
Send email to multiple recipients in Python (Python 3)
Convert psd file to png in Python
Sample script to trap signals in Python
I tried to implement PLSA in Python 2
To set default encoding to utf-8 in python
Epoch in Python
Discord in Python
Decorator to avoid UnicodeEncodeError in Python 3 print ()
How to work with BigQuery in Python
Sudoku in Python
DCI in Python
quicksort in python
Log in to Slack using requests in Python
nCr in python
N-Gram in Python
How to get a stacktrace in python
Programming in python
How to display multiplication table in python
Easy way to use Wikipedia in Python
How to extract polygon area in Python
How to check opencv version in python
Plink in Python