JerryYe的个人博客分享 http://blog.sciencenet.cn/u/JerryYe

博文

【Python】入门9——PyCharm脚本-HYCOM海洋数据批量下载

已有 3702 次阅读 2021-12-22 16:10 |个人分类:Python|系统分类:科研笔记

>>>利用Python库实现HYCOM海洋数据批量下载(备注:Chromedriver要对应浏览器版本号!)

=====================================================================

import time

import os

from selenium import webdriver

from selenium.webdriver.support.select import Select

driver = webdriver.Chrome('/Library/Frameworks/Python.framework/Versions/3.9/bin/chromedriver')

driver.get('http://ncss.hycom.org/thredds/ncss/grid/GLBu0.08/expt_19.1/2011/dataset.html')

#driver.get('https://ncss.hycom.org/thredds/ncss/grid/GLBy0.08/expt_93.0/dataset.html')

driver.implicitly_wait(10)

# click ele

driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[1]').click()

# click S,T,U,V

driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[2]').click()

driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[3]').click()

driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[4]').click()

driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[5]').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]').click()

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('2')

driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[1]').click()

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('84')

driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[3]').click()

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('94')

driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[3]/input[1]').click()

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('-2')

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')

# click single time, and input data_time

# 天

n = 0

for m in range(3,12+1):       #月份

    if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:

        month_num = 31     # 天数

    elif m == 4 or m == 6 or m == 9 or m == 11:

        month_num = 30     # 天数

    elif m == 2:

        month_num == 28    # 天数

    month = str(m).zfill(2)

    for i in range(1, month_num+1):

        Day = str(i).zfill(2)

        keys = '2011-' + month + '-' + Day + 'T00: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(3)

        #driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[3]/td/input[1]').click()

        time.sleep(20)

        dir = '/Users/qiujingyi/Downloads/'

        oldname = '2011' + '.nc'

        newname = '2011' + str(month) + str(Day) + '000000' + '.nc'

        os.rename(os.path.join(dir,oldname),os.path.join(dir,newname))

'''

# 天+小时

n = 0

for i in range(1, 31):

    Day = str(i).zfill(2)

    for j in range(0,21,3):

        Hour = str(j).zfill(2)

        keys = '2012-01-' + Day + 'T' + Hour + ':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(3)

        #driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[3]/td/input[1]').click()

        time.sleep(10)

        dir = '/Users/qiujingyi/Downloads/'

        oldname = '2012' + '.nc'

        newname = '201201' + str(Day) + str(Hour) + '0000' + '.nc'

        os.rename(os.path.join(dir,oldname),os.path.join(dir,newname))

'''




https://blog.sciencenet.cn/blog-2824237-1317703.html

上一篇:【Python】入门8——PyCharm脚本-打开网页输入账号密码登录
收藏 IP: 101.71.255.*| 热度|

1 王佳顺

该博文允许注册用户评论 请点击登录 评论 (1 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-28 19:45

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部