Last active
May 12, 2021 06:59
-
-
Save kakarukeys/426731b3bec40fcfacb27fb85b0b5580 to your computer and use it in GitHub Desktop.
automate login (tutorial 2)
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 os | |
import time | |
from PIL import Image | |
import requests | |
from bs4 import BeautifulSoup | |
# requires brotlipy, as the server uses brotli compression | |
username = os.environ["SSM_USERNAME"] | |
password = os.environ["SSM_PASSWORD"] | |
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", | |
} | |
LOGIN_PAGE_HEADERS = HEADERS.copy() | |
LOGIN_PAGE_HEADERS.update({ | |
"Referer": "https://www.ssm-einfo.my/index.php", | |
}) | |
IMG_HEADERS = HEADERS.copy() | |
IMG_HEADERS.update({ | |
"Accept": "image/webp,*/*", | |
"Referer": "https://www.ssm-einfo.my/index.php?id=login", | |
}) | |
LOGIN_POST_HEADERS = HEADERS.copy() | |
LOGIN_POST_HEADERS.update({ | |
"Content-Type": "application/x-www-form-urlencoded", | |
"Referer": "https://www.ssm-einfo.my/index.php?id=login", | |
}) | |
SEARCH_PAGE_HEADERS = HEADERS.copy() | |
SEARCH_PAGE_HEADERS.update({ | |
"Referer": "https://www.ssm-einfo.my/member/index.php?id=eAccountHome", | |
}) | |
BASE_URL = "https://www.ssm-einfo.my/" | |
if __name__ == "__main__": | |
with requests.Session() as session: | |
resp = requests.get(f"{BASE_URL}index.php?id=login", headers=LOGIN_PAGE_HEADERS) | |
time.sleep(5) | |
soup = BeautifulSoup(resp.content, features="html.parser") | |
token_value = soup.select('#loginForm input[name="csrf_token"]')[0]["value"] | |
captcha_img_url = BASE_URL + soup.select('#loginForm img[src^="random"]')[0]["src"] | |
resp = session.get(captcha_img_url, stream=True, headers=IMG_HEADERS) | |
with Image.open(resp.raw) as img: | |
img.show() | |
captcha_code = input("solve captcha:") | |
data = { | |
"returnURL": '', | |
"username": username, | |
"password": password, | |
"image": captcha_code, | |
"csrf_token": token_value, | |
"submit": '', | |
} | |
resp = session.post(f"{BASE_URL}index.php?id=proceedLogin", data=data, headers=LOGIN_POST_HEADERS) | |
time.sleep(5) | |
resp = session.get(f"{BASE_URL}member/index.php?id=uni", headers=SEARCH_PAGE_HEADERS) | |
soup = BeautifulSoup(resp.content, features="html.parser") | |
print(soup.select("#search_number")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment