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