用Python 下载hycom数据

David!Li 2021-05-14 09:30:42
本文利用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

...全文
4313 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

255

社区成员

发帖
与我相关
我的任务
社区描述
其他产品/厂家
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧