-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelenium.py
121 lines (93 loc) · 4.57 KB
/
Selenium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import chromedriver_autoinstaller
from pathlib import Path
import pandas as pd
import time
import os
from os.path import exists
import shutil
import csv
# The following 3 lines are for ubuntu only. If windows, please comments then to work well..
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 800))
display.start()
chromedriver_autoinstaller.install() # Check if the current version of chromedriver exists
# and if it doesn't exist, download it automatically,
# then add chromedriver to path
def getDownLoadedFileNameClose():
driver.close()
driver.switch_to.window(driver.window_handles[0])
def getDownLoadedFileName():
driver.execute_script("window.open()")
driver.switch_to.window(driver.window_handles[-1])
driver.get('chrome://downloads')
#driver.get_screenshot_as_file("page.png")
return driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').text")
downloadDir = f"{os.getcwd()}//"
preferences = {"download.default_directory": downloadDir,
"download.prompt_for_download": False,
"directory_upgrade": True,
"safebrowsing.enabled": True}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("prefs", preferences)
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
chrome_options.add_argument("--window-size=1200,1200")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Chrome(options = chrome_options)
# 打開目標網頁
driver.get("https://www.tpex.org.tw/zh-tw/mainboard/applying/status/company.html")
# 等待按鈕加載並按 data-format 屬性為 csv-u8 定位按鈕
wait = WebDriverWait(driver, 10) # 等待按鈕的時間
download_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-format='csv-u8']")))
# 滾動到按鈕並點擊
driver.execute_script("arguments[0].scrollIntoView(true);", download_button)
ActionChains(driver).move_to_element(download_button).click(download_button).perform()
# 等待下載完成
time.sleep(2) # 視下載速度調整時間
print("CSV completed!")
#driver.get_screenshot_as_file("page.png")
latestDownloadedFileName = getDownLoadedFileName()
time.sleep(2)
#driver.get_screenshot_as_file("page1.png")
getDownLoadedFileNameClose()
DownloadedFilename=''.join(latestDownloadedFileName).encode().decode("utf-8")
if DownloadedFilename != "TPEX-IPO-utf8.csv":
# Copy the file to "OTC.csv"
shutil.copy(DownloadedFilename, "TPEX-IPO-utf8.csv")
print(f"File '{DownloadedFilename}' copied to 'TPEX-IPO-utf8.csv'.")
print("Download completed...",downloadDir+'TPEX-IPO-utf8.csv')
# 移除原始檔案
os.remove(DownloadedFilename)
# 打開目標網頁
driver.get("https://www.twse.com.tw/zh/listed/listed/apply-listing.html")
# 等待按鈕加載並按 data-format 屬性為 csv-u8 定位按鈕
wait = WebDriverWait(driver, 10) # 等待按鈕的時間
download_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class='csv']")))
# 滾動到按鈕並點擊
driver.execute_script("arguments[0].scrollIntoView(true);", download_button)
ActionChains(driver).move_to_element(download_button).click(download_button).perform()
# 等待下載完成
time.sleep(2) # 視下載速度調整時間
print("CSV completed!")
#driver.get_screenshot_as_file("page.png")
latestDownloadedFileName = getDownLoadedFileName()
time.sleep(2)
#driver.get_screenshot_as_file("page1.png")
getDownLoadedFileNameClose()
DownloadedFilename=''.join(latestDownloadedFileName).encode().decode("utf-8")
if DownloadedFilename != "applylisting.csv":
# Copy the file to "OTC.csv"
shutil.copy(DownloadedFilename, "applylisting.csv")
print(f"File '{DownloadedFilename}' copied to 'applylisting.csv'.")
print("Download completed...",downloadDir+'applylisting.csv')
# 移除原始檔案
os.remove(DownloadedFilename)