Last active
May 12, 2021 06:59
-
-
Save kakarukeys/12270b02417898c301a967022f4371a8 to your computer and use it in GitHub Desktop.
automate login again (tutorial 10)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from PIL import Image | |
import requests | |
from bs4 import BeautifulSoup | |
HEADERS = { | |
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:88.0) Gecko/20100101 Firefox/88.0", | |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | |
"Accept-Language": "en-US,en;q=0.5", | |
"Accept-Encoding": "gzip, deflate, br", | |
"Connection": "keep-alive", | |
} | |
IMG_HEADERS = HEADERS.copy() | |
IMG_HEADERS.update({ | |
"Accept": "image/webp,*/*", | |
"Referer": "https://datawarehouse.dbd.go.th/login/en", | |
}) | |
LOGIN_POST_HEADERS = HEADERS.copy() | |
LOGIN_POST_HEADERS.update({ | |
"Referer": "https://datawarehouse.dbd.go.th/", | |
}) | |
BASE_URL = "https://datawarehouse.dbd.go.th/" | |
if __name__ == "__main__": | |
with requests.Session() as session: | |
requests.get(f"{BASE_URL}login/en", headers=HEADERS) | |
time.sleep(5) | |
resp = session.get(f"{BASE_URL}captcha", stream=True, headers=IMG_HEADERS) | |
with Image.open(resp.raw) as img: | |
img.show() | |
captcha_code = input("solve captcha:") | |
data = { | |
"captchaCode": captcha_code, | |
"lang": "en", | |
} | |
resp = session.post(f"{BASE_URL}authen4", data=data, headers=LOGIN_POST_HEADERS) | |
if resp.url == f"{BASE_URL}index": | |
soup = BeautifulSoup(resp.content, features="html.parser") | |
print(soup.select("#textStr")) | |
else: | |
print("login failed!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment