Last active
April 3, 2021 05:47
-
-
Save paperlefthand/9c8d767249cfc35f3ea274adca4ed292 to your computer and use it in GitHub Desktop.
奨学金返済状況スクレイピング(Python)
This file contains 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
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.support.ui import Select | |
import json | |
import time | |
URL = "https://scholar-ps.sas.jasso.go.jp/mypage/login_open.do" | |
with open('./setting.json') as f: | |
setting = json.load(f) | |
ID = setting["id"] | |
PASSWORD = setting["password"] | |
UNIV_NUM = setting["univ_num"] | |
options = Options() | |
options.add_argument('--headless') | |
# options.binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' | |
browser = webdriver.Chrome(options=options) | |
browser.get(URL) | |
try: | |
# input ID/password and wait 1 second | |
id = browser.find_element_by_id("userId") | |
id.send_keys(ID) | |
password = browser.find_element_by_id("password") | |
password.send_keys(PASSWORD) | |
time.sleep(1) | |
login_button = browser.find_element_by_id("login") | |
login_button.click() | |
# fill three text areas and wait 1 second | |
NUMS = UNIV_NUM.split('-') | |
ta1 = WebDriverWait(browser, 10).until( | |
EC.presence_of_element_located((By.ID, "syogkseibgKakunin_open_syogkseiBg1")) | |
) | |
ta1.send_keys(NUMS[0]) | |
ta2 = browser.find_element_by_id("kakuninInput_openToEntry_syogkseiBg2") | |
sel = Select(ta2) | |
sel.select_by_value(NUMS[1]) | |
ta3 = browser.find_element_by_id("syogkseibgKakunin_open_syogkseiBg3") | |
ta3.send_keys(NUMS[2]) | |
time.sleep(1) | |
submit_button = browser.find_element_by_id("syogkseibgKakunin_open_syogkseibgKakunin_submit") | |
submit_button.click() | |
# click target tab and save screenshot | |
tab = WebDriverWait(browser, 10).until( | |
EC.presence_of_element_located((By.ID, "syosaiJohoTab")) | |
) | |
tab.click() | |
# # Can't save element's screenshot by Chrome browser | |
# table = browser.find_element_by_css_selector("td.content-large-main-syosaiJoho-bgColor") | |
# table.screenshot('./screenshot.png') | |
page_width = browser.execute_script('return document.body.scrollWidth') | |
page_height = browser.execute_script('return document.body.scrollHeight') | |
browser.set_window_size(page_width, page_height) | |
browser.save_screenshot('./result.png') | |
browser.close() | |
finally: | |
browser.quit() |
This file contains 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
{ | |
"id": "XXX", | |
"password": "YYY", | |
"univ_num": "000-00-000000" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment