本文利用python的selenium對網頁進行自動化操作,從而以回圈的方式下載hycom官網NetcdfSubset中的資料
(1)在選擇想要的hycom資料集之后,選擇NetcdfSubset中的鏈接進入網頁下載界面。比如下方圖片:

該網頁只能選擇零散的資料下載,如果想要下載一年或者多年的資料,總不可能一個一個點吧。
(2)所以我這里選擇使用python的selenium進行多檔案下載處理。關于對selenium的說明和相應的瀏覽器驅動下載在這個https://blog.csdn.net/shiaohan/article/details/108834770 帖子里說的很清楚了。在安裝完相應的庫之后,如下圖,就可以開始撰寫:

(3)填上相應的網址,以防萬一在后面添加了driver.implicitly_wait(time)進行一段時間的等待
driver = webdriver.Chrome('D:\\chromedriver\\chromedriver.exe') # Optional argument, if not specified will search path.
driver.get('http://ncss.hycom.org/thredds/ncss/grid/GLBv0.08/expt_53.X/data/2008/dataset.html')
driver.implicitly_wait(20)
(4)接著就是網頁中其它框框的選擇,基本都不難。在選擇輸出格式時這里出現了下拉選擇框,我是用了select的功能,這個功能在這個https://www.cnblogs.com/w770762632/p/8745261.html 里面有很好的說明。
# click ele
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[2]').click()
# click S,T,U,V
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[6]').click()
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[7]').click()
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[8]').click()
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[9]').click()
driver.implicitly_wait(5)
# click Disable horizontal subsetting
driver.find_element_by_xpath('//*[@id="disableLLSubset"]').click()
# input lat,lon
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[1]/input[1]').clear()
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[1]/input[1]').send_keys('18')
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[1]').clear()
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[1]').send_keys('105')
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[3]').clear()
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[3]').send_keys('115')
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[3]/input[1]').clear()
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[3]/input[1]').send_keys('9')
driver.implicitly_wait(1)
# click vertical stride
driver.find_element_by_xpath('//*[@id="inputVerticalStride"]/span').click()
driver.implicitly_wait(1)
#click to add lon/lat variables
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[2]/div[12]/input').click()
# choose output format
s = driver.find_element_by_name('accept')
Select(s).select_by_value('netcdf')
(5)在把基本的選項弄完之后,接著就是利用回圈寫入時間進行批量下載了。考慮到網路波動等因素,我這里是逐月下載的,
# click single time ,and input data_time
n = 0
for i in range(1, 31):
n = n+1
N = str(i).zfill(2)
keys = '2008-01-' + N + 'T12:00:00Z'
print(keys)
driver.find_element_by_xpath('//*[@id="singleTimeSubset"]/input').clear()
driver.find_element_by_xpath('//*[@id="singleTimeSubset"]/input').send_keys(keys)
driver.implicitly_wait(10)
# click to submit
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[3]/td/input[1]').click()
time.sleep(120)
(6) END
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/283981.html
標籤:其他