-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_.py
165 lines (139 loc) · 8.78 KB
/
function_.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#funtion在這裡
#註1 :所有定義函式的引入變數都要有 [driver :源自於mainfile的webdriver]
from import_ import *
#初始化(清空) information.py 檔案
def init_file(path):
file = open(path, 'w')
file.write("store_data = []")
file.write("\n")
file.write("water_given_data = []")
file.close()
def check_del_module():
if 'data_' in sys.modules:
del sys.modules["data_"]
#利用webdriverwait函式等待直到找到指定xpath元素
def driver_wait_by_xpath(driver, x_path):
information = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, x_path)))
return information
def driver_wait_by_xpath_text(driver, x_path):
information = driver_wait_by_xpath(driver, x_path)
information = information.text
return information
#選取行政區
def locate_place(driver, x_path, value, id_):
#location_position = Select(driver.find_element_by_id(id_))
path = x_path + '/option[@value=' + value + ']'
driver_wait_by_xpath(driver, '/html/body/main/div/div[2]/div[1]/div/div[1]/a')
driver_wait_by_xpath(driver, path).click()
#取得案件編號
def get_case_num(driver, x_path):
case_num = driver_wait_by_xpath_text(driver, x_path)
#driver_wait_by_xpath(driver, '/html/body/main/div/div[2]/div[1]/div/div[1]/a')
#case_num = driver.find_element_by_xpath(x_path).text
return case_num
def sleep(times):
times = int(times)
time.sleep(times)
#將當前案件編號的資訊蒐集起來
#collect_information_condition_1: 有停水有降壓地區尚未復水的狀態
#collect_information_condition_2: 無停水有降壓地區尚未復水的狀態
#collect_information_condition_3: 有停水無降壓地區尚未復水的狀態
#collect_information_condition_4: 已復水
def collect_information_condition_1(driver, case__num):
start_water_cut_time_string = ['']
stop_water_cut_time_string = ['']
writefile_store_data(driver, '', case__num)
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[1]', '')
try:
driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span[2]')
start_water_cut_time_string[0] += driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span[1]').text + " ~ " + driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span[2]').text
writefile_store_data(driver, '', start_water_cut_time_string[0])
except:
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[1]', '')
try:
driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span[2]')
stop_water_cut_time_string[0] += driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span[1]').text + " ~ " + driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span[2]').text
writefile_store_data(driver, '', stop_water_cut_time_string[0])
except:
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[3]/div[1]/div[2]', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[3]/div[2]/div[2]', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[4]/div[1]/div[2]', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[4]/div[2]/div[2]', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[5]/div[4]/div', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[5]/div[7]/div', '')
writefile_store_data(driver, '', '未復水')
url = "https://wateroffmap.water.gov.tw/map/view/" + case__num
writefile_store_data(driver, 'more_information', '', url)
def collect_information_condition_2(driver, case__num):
start_water_cut_time_string = ['']
stop_water_cut_time_string = ['']
writefile_store_data(driver, '', case__num)
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[1]', '')
try:
driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span[2]')
start_water_cut_time_string[0] += driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span[1]').text + " ~ " + driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span[2]').text
writefile_store_data(driver, '', start_water_cut_time_string[0])
except:
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[1]', '')
try:
driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span[2]')
stop_water_cut_time_string[0] += driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span[1]').text + " ~ " + driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span[2]').text
writefile_store_data(driver, '', stop_water_cut_time_string[0])
except:
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span', '')
writefile_store_data(driver, '', 'None')
writefile_store_data(driver, '', 'None')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[4]/div[1]/div[2]', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[4]/div[2]/div[2]', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[5]/div[4]/div', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[5]/div[7]/div', '')
writefile_store_data(driver, '', '未復水')
url = "https://wateroffmap.water.gov.tw/map/view/" + case__num
writefile_store_data(driver, '', url)
def collect_information_condition_3(driver, case__num):
start_water_cut_time_string = ['']
stop_water_cut_time_string = ['']
writefile_store_data(driver, '', case__num)
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[1]', '')
try:
driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span[2]')
start_water_cut_time_string[0] += driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span[1]').text + " ~ " + driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span[2]').text
writefile_store_data(driver, '', start_water_cut_time_string[0])
except:
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[1]/div[2]/span', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[1]', '')
try:
driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span[2]')
stop_water_cut_time_string[0] += driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span[1]').text + " ~ " + driver.find_element_by_xpath('/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span[2]').text
writefile_store_data(driver, '', stop_water_cut_time_string[0])
except:
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[2]/div[2]/div[2]/span', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[3]/div[1]/div[2]', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[3]/div[2]/div[2]', '')
writefile_store_data(driver, '', 'None')
writefile_store_data(driver, '', 'None')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[5]/div[4]/div', '')
writefile_store_data(driver, '/html/body/div/div/div[1]/div[3]/div[2]/div[5]/div[7]/div', '')
writefile_store_data(driver, '', '未復水')
url = "https://wateroffmap.water.gov.tw/map/view/" + case__num
writefile_store_data(driver, '', url)
def collect_information_condition_4(driver, case__num):
writefile_water_given_data(driver, case__num)
writefile_water_given_data(driver, '已復水')
url = "https://wateroffmap.water.gov.tw/map/view/" + case__num
writefile_water_given_data(driver, url)
#
#x_path: 搜尋網頁元素的路徑
def writefile_store_data(driver, x_path, output_text):
from data_ import store_data
if x_path != '' :
catch_text = driver.find_element_by_xpath(x_path).text
store_data.append(catch_text)
else:
store_data.append(output_text)
def writefile_water_given_data(driver, output_text):
from data_ import water_given_data
water_given_data.append(output_text)