commit ace49a779572011296ec1aadb0f64381827caf81 Author: hochonin93 Date: Fri Oct 20 16:12:37 2023 +0800 first commit diff --git a/main.py b/main.py new file mode 100644 index 0000000..bab8e41 --- /dev/null +++ b/main.py @@ -0,0 +1,185 @@ +from PIL import Image +import platform +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.common.keys import Keys +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.support.ui import Select +from selenium.webdriver.common.action_chains import ActionChains +import time +import ddddocr +import sys +from openpyxl import load_workbook + + +def pic_ocr(path: str): + ocr = ddddocr.DdddOcr() + with open(path, 'rb') as f: + img_bytes = f.read() + + res = ocr.classification(img_bytes) + return res + + +def start(): + # 開始進入作業流程 + options = Options() + options.add_argument("--disable-notifications") + + try: + return webdriver.Chrome('./chromedriver', chrome_options=options) + except WebDriverException as ex: + if 'Chrome version must be' in str(ex): + print('Chrome 驅動版本不支援,請更新 Chrome (%s)' % ex) + else: + print('Chrome 驅動出錯 (%s)' % ex) + sys.exit(1) + + +def download_captcha(driver): + # 驗證碼坐標 + captcha = driver.find_element(By.ID, 'checkCode2') + driver.get_screenshot_as_file("captcha.png") + location = captcha.location # 驗證碼圖片位置 + size = captcha.size # 驗證碼圖片大小 + + # 截圖高度調整 + x = 900 + y = 1138 + width = size['width'] + height = size['height'] + x1 = x + y1 = y + x2 = x + width+130 + y2 = y + height+30 + + # 透過坐標截圖 + screenshot = Image.open('captcha.png') + screenshot = screenshot.convert('RGB') + captcha_photo = screenshot.crop((x1, y1, x2, y2)) + + captcha_photo.save('captcha.jpg', 'JPEG') + + +def is_login_success(driver): + # 檢查是否進入修改畫面 + page_source_code = driver.page_source + if page_source_code.find('圖形驗證碼錯誤') == -1: + return True + + return False + + +def is_change_pwd(driver): + # 檢查是否成功更改密碼 + page_source_code = driver.page_source + if page_source_code.find('密碼更新完成') == -1: + return True + + return False + + +def login(driver, id, pwd): + # 統一編號 + ban = driver.find_elements(By.NAME, 'BAN') + ban[1].send_keys(id) + + # 申報密碼 + pw = driver.find_element(By.NAME, 'PW') + pw.send_keys(pwd) + + # 圖形驗證碼 + download_captcha(driver) + captcha = pic_ocr('./captcha.jpg') + print(f'驗證碼:{captcha}') + pw = driver.find_element(By.ID, 'captcha2') + pw.send_keys(captcha) + pw.send_keys(Keys.ENTER) + + +def change_pwd(driver, new_pwd, old_pwd): + # 新密碼 + pw1 = driver.find_element(By.ID, 'PW1') + pw1.send_keys(new_pwd) + + # 新密碼確認 + pw2 = driver.find_element(By.ID, 'PW2') + pw2.send_keys(new_pwd) + + # 新密碼確認 + pw3 = driver.find_element(By.ID, 'PW3') + pw3.send_keys(old_pwd) + + # 確定 + enter = driver.find_element(By.NAME, 'B1') + enter.click() + + time.sleep(3) + + con_enter = driver.find_element( + By.XPATH, '/html/body/div[2]/div/div/div[2]/button[2]') + con_enter.click() + + +data = { + '統一編號': None, + '密碼': None, + '暫時密碼': None +} + +url = 'https://svc.tax.nat.gov.tw/svc/fchoice.jsp' +change_password_url = 'https://svc.tax.nat.gov.tw/svc/PwApplyUpdatePw.jsp' + +# 讀取 Excel 檔案 +filename = '密碼變更.xlsx' +wb = load_workbook(filename, read_only=False) +sheet = wb[wb.active.title] + +# 讀取Excel內容 +for row in sheet.iter_rows(min_row=2, max_col=3, max_row=10): + # 導入資料 + keysList = [key for key in data] + for i, cell in enumerate(row): + row_num = cell.row + data[keysList[i]] = cell.value + + print(data['統一編號']) + print(data['密碼']) + print(data['暫時密碼']) + + if data['統一編號'] == None: + break + + driver = start() + driver.get(url) + + login(driver, data['統一編號'], data['密碼']) + + time.sleep(3) + + if is_login_success(driver): + driver.get(change_password_url) + else: + print('進入失敗') + + time.sleep(3) + + change_pwd(driver, data['暫時密碼'], data['密碼']) + + time.sleep(3) + + # 前往首頁 + driver.get(url) + + login(driver, data['統一編號'], data['暫時密碼']) + + if is_login_success(driver): + driver.get(change_password_url) + else: + print('進入失敗') + + change_pwd(driver, data['密碼'], data['暫時密碼']) + + driver.close() + print(f"{data['統一編號']}已更新成功") + time.sleep(10) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1dbaedc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +Pillow==9.5.0 +selenium +ddddocr \ No newline at end of file