-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusgs_query.py
212 lines (170 loc) · 7.34 KB
/
usgs_query.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import requests
import pandas as pd
import lxml.html as lh
from html_table_extractor.extractor import Extractor
# -*- coding: utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import Firefox
######################
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
######################
import os
#soup = BeautifulSoup(url)
#Query build:
#1 - Columns to show
value_1 = "realdate" # Date(MM/DD/YYYY)
value_2 = "stat" # Station Number
#VARS:
VARS_NAME = ['Depth',
'Discrete Chlorophyll',
'Chlorophyll a/a+PHA ratio',
'Fluorescence',
'Calculated Chlorophyll',
'Discrete Oxygen',
'Oxygen electrode output',
'Oxygen Saturation %',
'Calculated Oxygen',
'Discrete SPM',
'Optical Backscatter',
'Calculated SPM',
'Measured Extinction Coeff',
'Calculated Extinction Coeff',
'Salinity',
'Temperature',
'Sigma-t',
'Nitrite',
'Nitrate+Nitrite',
'Ammonium',
'Phosphate',
'Silicate'
]
value_1 = "-2" # Date(MM/DD/YYYY)
value_2 = "-1" # Station Number
VARS = {
"-2": "realdate",
"-1": "stat",
'1': 'depth',
'2': 'dscrchl',
'3': 'chlrat',
'4': 'fluor',
'5': 'calcchl',
'6': 'dscroxy',
'7': 'oxy',
'8': 'oxysat',
'9': 'calcoxy',
'10': 'dscrspm',
'11': 'obs',
'12': 'calcspm',
'13': 'dscrexco',
'14': 'excoef',
'15': 'salin',
'16': 'temp',
'17': 'sigt',
'18': 'no2',
'19': 'no32',
'20': 'nh4',
'21': 'po4',
'22': 'si',
}
for i in range(0,22):
print('{} - {}'.format(str(i+1),VARS_NAME[i]))
print('Digite Quit para encerrar a escolha de variáveis')
choose = ''
query = [value_1,value_2]
while choose.upper() != 'QUIT':
choose = input('Código da variável: ')
if choose.upper() != 'QUIT':
query.append(choose)
def download_download_usgs(outputfile):
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.dir", dir_out)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream")
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.useDownloadDir", True)
fp.set_preference("browser.helperApps.alwaysAsk.force", False)
fp.set_preference("browser.download.manager.alertOnEXEOpen", False)
fp.set_preference("browser.download.manager.closeWhenDone", True)
fp.set_preference("browser.download.manager.showAlertOnComplete", False)
fp.set_preference("browser.download.manager.useWindow", False)
fp.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False)
fp.set_preference("pdfjs.disabled", True)
fp.set_preference("browser.link.open_newwindow.override.external", True)
#C:\Program Files (x86)\Mozilla Firefox
binary = FirefoxBinary("C://Program Files//Mozilla Firefox//firefox.exe")
driver = webdriver.Firefox(firefox_profile=fp,firefox_binary=binary, executable_path='C://geckodriver')
wait = WebDriverWait(driver, 10)
driver.minimize_window()
url = 'https://sfbay.wr.usgs.gov/access/wqdata/query/expert.html'
driver.get(url)
time.sleep(1)
# 1 - Columns to Show
for var in query:
if int(var) <= 17:
driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/th/table/tbody/tr/th[2]/span/form/table/tbody/tr/td/table/tbody/tr[1]/td[1]/font/input[@value='"+VARS[var] +"']").click()
elif 17 < int(var) & int(var) <= 20:
driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/th/table/tbody/tr/th[2]/span/form/table/tbody/tr/td/table/tbody/tr[1]/td[1]/table/tbody/tr/td[1]/font/input[@value='"+ VARS[var] +"']").click()
else:
driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/th/table/tbody/tr/th[2]/span/form/table/tbody/tr/td/table/tbody/tr[1]/td[1]/table/tbody/tr/td[2]/font/input[@value='"+ VARS[var] +"']").click()
# 3 - Sort by:
name1 = 'sort1'
value_3 = 'fulldate'
name2 = 'sort2'
value_4 = 'stat'
name3 = 'sort3'
value_5 = 'depth'
driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/th/table/tbody/tr/th[2]/span/form/table/tbody/tr/td/table/tbody/tr[4]/td[1]/select/option[2]").click()
driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/th/table/tbody/tr/th[2]/span/form/table/tbody/tr/td/table/tbody/tr[5]/td[1]/select/option[3]").click()
driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/th/table/tbody/tr/th[2]/span/form/table/tbody/tr/td/table/tbody/tr[6]/td[1]/select/option[5]").click()
#4 - Table Format
name4 = 'out'
value = 'html' #comma
name5 = 'param'
name6 = 'maxrow' # 10, 50, 100, 500, 1000, "99999"
driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/th/table/tbody/tr/th[2]/span/form/table/tbody/tr/td/table/tbody/tr[8]/th/table/tbody/tr/td/select/option[1]").click()
driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/th/table/tbody/tr/th[2]/span/form/table/tbody/tr/td/table/tbody/tr[8]/th/table/tbody/tr/td/font/select/option[5]").click()
#5 - Submit
type7 = 'submit'
name7 = "Query Database"
driver.find_element_by_xpath("/html/body/table[2]/tbody/tr/th/table/tbody/tr/th[2]/span/form/table/tbody/tr/td/table/tbody/tr[8]/th/table/tbody/tr/th/input[2]").click()
alert = driver.switch_to_alert()
alert.accept()
# Get Data:
info = wait.until(ec.visibility_of_element_located(( By.XPATH,"/html/body/form[2]/table/tbody/tr/td[1]"))).text
noData = info.split('out of ')[1][:-10]
countData = info.split('out of ')[0].split('to ')[1][:-1]
subname = '10p'
n = 1
w = 1
while countData != noData:
table = driver.find_element_by_xpath('/html/body/table[3]')
extractor = Extractor(table.get_attribute("outerHTML")).parse()
extractor.write_to_csv(path='.')
if float(countData) > float(noData)*0.1*n:
n += 1
subname = str(n)+'0p'
out_file = subname + '_' + outputfile
if os.path.exists(os.path.join(os.getcwd(),out_file)):
df = pd.read_csv(os.path.join(os.getcwd(),out_file))
data = pd.read_csv(os.path.join(os.getcwd(),'output.csv'), encoding = 'cp1252')
df = pd.concat([df,data], ignore_index = True)
df.to_csv(os.path.join(os.getcwd(),out_file))
else:
data = pd.read_csv(os.path.join(os.getcwd(),'output.csv'), encoding = 'cp1252')
data.to_csv(os.path.join(os.getcwd(),out_file))
info = wait.until(ec.visibility_of_element_located(( By.XPATH,"/html/body/form[2]/table/tbody/tr/td[1]"))).text
countData = info.split('out of ')[0].split('to ')[1][:-1]
print(countData)
print(w)
w+=1
try:
driver.find_element_by_xpath("/html/body/form[2]/table/tbody/tr/td[1]/input").click()
except:
print("Programa encerrado. Checar se todos os dados foram baixados")
return Data
df = download_usgs('outputData.csv')