|
| 1 | +### 2025/12/23 test ok |
1 | 2 | import gspread |
2 | 3 | from oauth2client.service_account import ServiceAccountCredentials |
3 | 4 | from selenium import webdriver |
|
16 | 17 | load_dotenv() |
17 | 18 |
|
18 | 19 | scope = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/drive'] |
19 | | -creds = ServiceAccountCredentials.from_json_keyfile_name('banklinker-473405-6be3b03228c7.json', scope) |
| 20 | +creds = ServiceAccountCredentials.from_json_keyfile_name('TOUR_JSON_FILE_NAME.json', scope) |
20 | 21 | client = gspread.authorize(creds) |
21 | 22 | sheet = client.open("Bank").worksheet("總明細") |
22 | 23 |
|
23 | | -HEADLESS = True |
| 24 | +HEADLESS = False |
24 | 25 |
|
25 | 26 | chrome_options = Options() |
26 | 27 | if HEADLESS: |
@@ -143,51 +144,47 @@ def CathaySpider(): |
143 | 144 | EC.element_to_be_clickable((By.XPATH, "//button[@type='button' and @class='btn no-print btn-fill js-login btn btn-fill w-100 u-pos-relative' and @onclick='NormalDataCheck()']")) |
144 | 145 | ) |
145 | 146 | driver.execute_script("arguments[0].click();", loginButton) |
| 147 | + time.sleep(10) |
146 | 148 |
|
147 | | - link_element = WebDriverWait(driver, 20).until( |
148 | | - EC.visibility_of_element_located((By.XPATH, "//a[contains(@onclick, 'AutoGoMenu') and @class='link u-fs-14']")) |
| 149 | + ###TWD |
| 150 | + button_element = WebDriverWait(driver, 20).until( |
| 151 | + EC.visibility_of_element_located((By.CSS_SELECTOR, 'button[data-evt="home_twd_overview"]')) |
149 | 152 | ) |
150 | | - |
151 | | - Cathay.main_account = link_element.text |
152 | | - print("CATHAYAccount:", Cathay.main_account) |
153 | | - |
154 | | - balance_element = WebDriverWait(driver, 20).until( |
155 | | - EC.visibility_of_element_located((By.ID, "TD-balance")) |
156 | | - ) |
157 | | - balance_text = balance_element.text |
158 | | - Cathay.cash = int(balance_text.replace(",", "")) # 先去掉逗號,再轉換為整數 |
159 | | - print("CATHAYcash:", Cathay.cash) |
160 | | - |
161 | | - tabFTD = WebDriverWait(driver, 20).until( |
162 | | - EC.element_to_be_clickable((By.ID, "tabFTD")) |
| 153 | + raw_text = button_element.text |
| 154 | + clean_text = raw_text.replace("TWD", "").replace(",", "").strip() |
| 155 | + Cathay.cash = int(clean_text) |
| 156 | + print(f"CATHAY_TWD: {Cathay.cash}") |
| 157 | + |
| 158 | + ###Foreign |
| 159 | + foreign_currency_element = WebDriverWait(driver, 20).until( |
| 160 | + EC.visibility_of_element_located((By.CSS_SELECTOR, 'button[data-evt="home_foreign_currency_overview"]')) |
163 | 161 | ) |
164 | | - driver.execute_script("arguments[0].click();", tabFTD) |
165 | | - |
166 | | - balance_element = WebDriverWait(driver, 20).until( |
167 | | - EC.visibility_of_element_located((By.ID, "FTD-balance")) |
168 | | - ) |
169 | | - balance_text = balance_element.text |
170 | | - Cathay.exchange = int(balance_text.replace(",", "")) # 先去掉逗號,再轉換為整數 |
171 | | - print("CATHAYexchange:", Cathay.exchange) |
172 | | - |
173 | | - tabFUND = WebDriverWait(driver, 20).until( |
174 | | - EC.element_to_be_clickable((By.ID, "tabFUND")) |
| 162 | + foreign_currency_text = foreign_currency_element.text |
| 163 | + clean_text = foreign_currency_text.replace("TWD", "").replace(",", "").strip().split()[0] |
| 164 | + Cathay.exchange = int(clean_text) |
| 165 | + print("CATHAYForeign:", Cathay.exchange) |
| 166 | + |
| 167 | + ###STOCK |
| 168 | + xpath_selector = "//p[text()='投資']/parent::div/following-sibling::div[@class='css-iu1euh']/p" |
| 169 | + |
| 170 | + investment_element = WebDriverWait(driver, 20).until( |
| 171 | + EC.visibility_of_element_located((By.XPATH, xpath_selector)) |
175 | 172 | ) |
176 | | - driver.execute_script("arguments[0].click();", tabFUND) |
177 | | - |
178 | | - fund_balance_element = WebDriverWait(driver, 20).until( |
179 | | - EC.visibility_of_element_located((By.ID, "FUND-balance")) |
180 | | - ) |
181 | | - fund_balance_text = fund_balance_element.text |
182 | | - |
183 | | - # 移除逗號並轉換為數字 |
184 | | - Cathay.stock = int(fund_balance_text.replace(",", "")) # 先去掉逗號,再轉換為整數 |
| 173 | + |
| 174 | + # 2. 獲取文字並清理 |
| 175 | + investment_text = investment_element.text |
| 176 | + clean_text = investment_text.replace("TWD", "").replace(",", "").strip().split()[0] |
| 177 | + Cathay.stock = int(clean_text) |
185 | 178 | print("CATHAYstock:", Cathay.stock) |
186 | 179 |
|
| 180 | + ###LOGOUT |
187 | 181 | logout_button = WebDriverWait(driver, 20).until( |
188 | | - EC.element_to_be_clickable((By.XPATH, "//a[@onclick='IsNeedCheckReconcil()']")) |
| 182 | + EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[data-evt="onlinebanking-logout"]')) |
189 | 183 | ) |
190 | | - driver.execute_script("arguments[0].click();", logout_button) |
| 184 | + |
| 185 | + logout_button.click() |
| 186 | + |
| 187 | + |
191 | 188 | def LineSpider(): |
192 | 189 | driver.get("https://accessibility.linebank.com.tw/transaction") |
193 | 190 | wait = WebDriverWait(driver, 20) |
@@ -260,7 +257,7 @@ def JudgeColor(SheetRow, row): |
260 | 257 | cash_diff, exchange_diff, stock_diff, assets_diff, " ", |
261 | 258 | Esun.main_account, Esun.cash, Esun.exchange, Esun.stock, " ", |
262 | 259 | Cathay.main_account, Cathay.cash, Cathay.exchange, Cathay.stock, " ", |
263 | | - Line.cash, Line.exchange, Line.stock |
| 260 | + Line.main_account, Line.cash, Line.exchange, Line.stock |
264 | 261 | ], 3) |
265 | 262 |
|
266 | 263 | G3_value = int(sheet.cell(3, 7).value) |
|
0 commit comments