Skip to content

Commit 0928f28

Browse files
committed
v1.1.1
1 parent 0262a89 commit 0928f28

1 file changed

Lines changed: 37 additions & 40 deletions

File tree

main.py

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
### 2025/12/23 test ok
12
import gspread
23
from oauth2client.service_account import ServiceAccountCredentials
34
from selenium import webdriver
@@ -16,11 +17,11 @@
1617
load_dotenv()
1718

1819
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)
2021
client = gspread.authorize(creds)
2122
sheet = client.open("Bank").worksheet("總明細")
2223

23-
HEADLESS = True
24+
HEADLESS = False
2425

2526
chrome_options = Options()
2627
if HEADLESS:
@@ -143,51 +144,47 @@ def CathaySpider():
143144
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()']"))
144145
)
145146
driver.execute_script("arguments[0].click();", loginButton)
147+
time.sleep(10)
146148

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"]'))
149152
)
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"]'))
163161
)
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))
175172
)
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)
185178
print("CATHAYstock:", Cathay.stock)
186179

180+
###LOGOUT
187181
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"]'))
189183
)
190-
driver.execute_script("arguments[0].click();", logout_button)
184+
185+
logout_button.click()
186+
187+
191188
def LineSpider():
192189
driver.get("https://accessibility.linebank.com.tw/transaction")
193190
wait = WebDriverWait(driver, 20)
@@ -260,7 +257,7 @@ def JudgeColor(SheetRow, row):
260257
cash_diff, exchange_diff, stock_diff, assets_diff, " ",
261258
Esun.main_account, Esun.cash, Esun.exchange, Esun.stock, " ",
262259
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
264261
], 3)
265262

266263
G3_value = int(sheet.cell(3, 7).value)

0 commit comments

Comments
 (0)