Try logging in to qiita with Python

I logged in to qiita as an introduction to web scraping. When you click the login button, it seems that the following data is sent to https://qiita.com/login.

--utf-8: Fixed with ✓ --authenticity_token: It seems to be issued when you set up a session: --identity: email address or user ID --password: password

So all you need to get in your code is authenticity_token. I tried to get it using the Beautiful Soup I just learned.

def get_authenticity_token(session, login_url):
    response = session.get(login_url)
    response.encoding = response.apparent_encoding
    bs = BeautifulSoup(response.text, 'html.parser')
    authenticity_token = str(bs.find(attrs={'name':'authenticity_token'}).get('value'))
    return authenticity_token

When you actually log in, it will be as follows.

import requests
import os
from bs4 import BeautifulSoup


user_name = 'user_name'
user_password = 'user_password'
login_url = 'https://qiita.com/login'


login_form = {
    'utf-8':'✓',
    'authenticity_token':'token',
    'identity':user_name,
    'password':user_password
}
    

def get_authenticity_token(session, login_url):
    response = session.get(login_url)
    response.encoding = response.apparent_encoding
    bs = BeautifulSoup(response.text, 'html.parser')
    authenticity_token = str(bs.find(attrs={'name':'authenticity_token'}).get('value'))
    return authenticity_token


if __name__ == '__main__':
    session = requests.Session()
    authenticity_token = get_authenticity_token(session, login_url)
    login_form['authenticity_token'] = authenticity_token
    session.post(login_url, login_form)

Recommended Posts

Try logging in to qiita with Python
[First API] Try to get Qiita articles with Python
Try to operate Facebook with Python
Try to calculate Trace in Python
Try to reproduce color film with Python
Try working with binary data in Python
How to work with BigQuery in Python
To work with timestamp stations in Python
Try scraping with Python.
Logging properly in Python
Try gRPC in Python
Try 9 slices in Python
First steps to try Google CloudVision in Python
Try to implement Oni Maitsuji Miserable in python
Try to calculate a statistical problem in Python
3.14 π day, so try to output in Python
[REAPER] How to play with Reascript in Python
Try auto to automatically price Enums in Python 3.6
Convert PDFs to images in bulk with Python
Try to solve the man-machine chart with Python
Try to draw a life curve with python
Try to make a "cryptanalysis" cipher with Python
Try to automatically generate Python documents with Sphinx
Log in to Yahoo Business with Selenium Python
Try to calculate RPN in Python (for beginners)
Try working with Mongo in Python on Mac
How to use tkinter with python in pyenv
Try to make a dihedral group with Python
Try using ChatWork API and Qiita API in Python
Try to detect fish with python + OpenCV2.4 (unfinished)
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
Try to factorial with recursion
Connect to BigQuery with Python
How to convert / restore a string with [] in python
Try to solve the programming challenge book with python3
Scraping with selenium in Python
Try to make a Python module in C language
To flush stdout in Python
Working with LibreOffice in Python
Try to make a command standby tool with python
How to do hash calculation with salt in Python
Scraping with chromedriver in python
Explain in detail how to make sounds with python
Try to understand Python self
Try to improve your own intro quiz in Python
Debugging with pdb in Python
Try to solve the internship assignment problem with Python
Try Python output with Haxe 3.2
Login to website in Python
Connect to Wikipedia with Python
Post to slack with Python 3
Try implementing associative memory with Hopfield network in Python
How to run tests in bulk with Python unittest
Try to operate DB with Python and visualize with d3
Try embedding Python in a C ++ program with pybind11
Working with sounds in Python
Scraping with Selenium in Python
Convert the image in .zip to PDF with Python
Super Primer to python-Getting started with python3.5 in 3 minutes
Try LINE Notify in Python
I was addicted to scraping with Selenium (+ Python) in 2020