Skip to content

Instantly share code, notes, and snippets.

@marethyu
Created August 26, 2024 06:43
Show Gist options
  • Save marethyu/82ad4b5bb13da26042adef3e2d17337c to your computer and use it in GitHub Desktop.
Save marethyu/82ad4b5bb13da26042adef3e2d17337c to your computer and use it in GitHub Desktop.
import requests
from bs4 import BeautifulSoup
LOGIN_URL = 'https://bunpro.jp/users/sign_in'
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'accept-encoding': 'gzip, deflate, br, zstd',
'accept-language': 'en-US,en;q=0.9',
'cache-control': 'max-age=0',
'content-length': '248',
'content-type': 'application/x-www-form-urlencoded',
'origin': 'https://bunpro.jp',
'priority': 'u=0, i',
'referer': LOGIN_URL,
'sec-ch-ua': '"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"',
'sec-ch-ua-mobile': '?1',
'sec-ch-ua-platform': '"Android"',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'same-origin',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Mobile Safari/537.36'
}
payload = {
'utf8': '✓',
'user[email]': '...',
'user[password]': '...',
'user[remember_me]': '1',
'commit': 'Log In'
}
with requests.Session() as session:
response = session.get(LOGIN_URL)
soup = BeautifulSoup(response.text, 'lxml')
payload['authenticity_token'] = soup.select_one('meta[name="csrf-token"]')['content']
headers['cookie'] = '; '.join([x.name + '=' + x.value for x in response.cookies])
response = session.post(LOGIN_URL, data=payload, headers=headers, allow_redirects=False)
assert response.status_code == 302
print(response.cookies.get_dict())
r = session.get('https://bunpro.jp/grammar_points/1', stream=True, cookies=response.cookies.get_dict())
print(r.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment