>>>利用Python库实现HYCOM海洋数据批量下载(备注:Chromedriver要对应浏览器版本号!)6 J7 `7 `, P. q. j* u% B
=====================================================================
" M1 ~% b* W) E& M5 }% c$ e# I$ dimport time- t# G1 ^1 N/ i4 j
import os
- u; b+ t) J3 h! k0 x$ efrom selenium import webdriver
( q4 X: c1 a+ e; J1 rfrom selenium.webdriver.support.select import Select
! G7 Q0 A* l) U2 I6 s4 jdriver = webdriver.Chrome('/Library/Frameworks/Python.framework/Versions/3.9/bin/chromedriver')
& j: q \4 _, {9 P' k& o; tdriver.get('www.52ocean.cn')2 o! J) b" p! s1 j7 d
#driver.get('www.52ocean.cn')
j; z; a1 G/ k9 Xdriver.implicitly_wait(10)9 m- g& G. V' Q! ^3 }
# click ele3 s E' F, n" L' n- W( T
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[1]').click()% R: {- @4 t. }7 K! x, V
# click S,T,U,V9 B6 Y$ P* b% I1 d9 M R
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[2]').click()+ O4 L3 `1 S+ z1 R# g4 q
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[3]').click(), e3 w9 F: H5 V1 H. E+ A
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[4]').click()8 _* H( @' ~8 N- R7 z' U
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[5]').click()
2 R6 m* E1 J& ~9 c! w% d8 ^3 g3 p f5 |driver.implicitly_wait(5)
V% b8 n) }# u, D# click Disable horizontal subsetting' ^/ O2 ^/ n2 f! ~
driver.find_element_by_xpath('//*[@id="disableLLSubset"]').click()* @* b5 c. u' N: m2 x
# input lat,lon5 a5 C/ K o, N7 N% p6 x8 G4 r
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[1]/input[1]').click()
& O7 O9 R" T$ q5 _: Rdriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[1]/input[1]').clear()
/ r5 }( l2 {! k' d2 r3 Gdriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[1]/input[1]').send_keys('2')
1 K4 a# D, e* X% `, u; M( l9 Rdriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[1]').click()
7 X& L% @0 Y6 A! @. A s3 M x8 xdriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[1]').clear()
/ i* S8 z0 `2 N0 N" y8 Y) bdriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[1]').send_keys('84')4 I6 J1 ?5 G: ?0 i1 k3 c" O+ k
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[3]').click()
) c; e0 {; g* o' Q% q0 adriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[3]').clear()% \) U3 J$ a# |( q1 p9 L
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[3]').send_keys('94')
- T6 w% M9 f; F& Edriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[3]/input[1]').click()
* b$ N" U3 S% Z2 A# c! ]( Bdriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[3]/input[1]').clear()
) d5 c( r& J3 ddriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[3]/input[1]').send_keys('-2')9 c; l( H3 H9 g1 \
driver.implicitly_wait(1)) f9 P2 P9 p, c% e$ T! z n) d
# click vertical stride1 s/ f* E# U e4 L* a' g6 [5 {
driver.find_element_by_xpath('//*[@id="inputVerticalStride"]/span').click()7 G4 W" I- |: P$ y2 L0 w4 F
driver.implicitly_wait(1)
' W7 w3 H% {. j9 J5 v# click to add lon/lat variables
- ?; }$ t( X% i3 i) I. G5 G' j- F. ndriver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[2]/div[12]/input').click()6 `1 O1 A" r- R1 ~/ s7 q
# choose output format3 I( d- Q# d$ m4 t! p
s = driver.find_element_by_name('accept') Q3 a. k0 U, |1 T' G+ W
Select(s).select_by_value('netcdf')7 ~7 N. _9 B% d' z* {! w3 D3 H0 c. B
# click single time, and input data_time0 W! t2 }7 u- A$ F1 n
# 天/ x0 N/ x1 U+ O4 F* L
n = 07 ^) ]. y" d- h5 \' K, a
for m in range(3,12+1): #月份
. h( F# m: N. z5 A( ~' J# F# ~9 \ if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:
) R# M% _* `) D' x month_num = 31 # 天数) f3 i; C$ g; B- S$ u) V
elif m == 4 or m == 6 or m == 9 or m == 11:7 [0 a0 f& \$ T9 }' _6 e5 G
month_num = 30 # 天数
" C1 r$ p; x3 ]$ B% p elif m == 2:
! `0 Q( n$ H" g) `3 ^ R month_num == 28 # 天数
- F* O7 m. k6 h; U4 E! ` month = str(m).zfill(2)
; h) l, j0 Y4 I. @ for i in range(1, month_num+1):
7 W1 y0 ^( [$ M- B Day = str(i).zfill(2)" ^3 X& u5 U4 ]# d; x/ p# u- D9 I
keys = '2011-' + month + '-' + Day + 'T00:00:00Z' # 日期
: F/ U$ l+ n6 e w# T5 L2 m print(keys)9 l% ^! ]. ~; G. Y6 U6 s/ m
driver.find_element_by_xpath('//*[@id="singleTimeSubset"]/input').clear()
- C/ `1 s# Q ]$ O$ }' b# W driver.find_element_by_xpath('//*[@id="singleTimeSubset"]/input').send_keys(keys)* F5 t- N4 Q! V1 d. h& Z H
driver.implicitly_wait(10)5 Y/ R+ b( m# c2 g( M+ m6 l! b
# click to submit
% q( T& b2 M# }+ f0 ^ driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[3]/td/input[1]').click()
, D. C1 V" D7 m8 W+ p' }, M #time.sleep(3)7 F7 ?& c% } R9 [; U- r7 k( O9 m
#driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[3]/td/input[1]').click()! ~, L. t( i' J* }/ F; a$ |, o% i" F
time.sleep(20)
0 M4 c# Y6 f' P dir = '/Users/qiujingyi/Downloads/'
( z- M2 t! B9 ^ oldname = '2011' + '.nc'
/ k9 |8 j3 o2 x% N: K1 y* E5 Y6 c6 p newname = '2011' + str(month) + str(Day) + '000000' + '.nc'( {, }. O% V0 [6 q7 i) w
os.rename(os.path.join(dir,oldname),os.path.join(dir,newname))7 W; o# K( e; f2 s
'''
4 a) a* m+ |3 A. w4 `# 天+小时) @( q9 t8 `+ b9 A- G# O
n = 0
4 c+ E* f$ j- a, {7 ?, Nfor i in range(1, 31):
5 \7 `& p4 K6 M* w# P Day = str(i).zfill(2)
! M8 |5 N. T3 X9 l$ n G for j in range(0,21,3):5 ?' e2 A7 N1 \# a3 w
Hour = str(j).zfill(2)6 V7 ~) x0 e4 y4 Q1 s/ E2 q* y
keys = '2012-01-' + Day + 'T' + Hour + ':00:00Z': P& ^. [6 e9 G# ~/ _9 l) S; g8 z
print(keys)
1 b$ Z- n5 Y) ] k% {: C driver.find_element_by_xpath('//*[@id="singleTimeSubset"]/input').clear()
0 c6 U& J% E( L3 g) O9 r0 @5 j driver.find_element_by_xpath('//*[@id="singleTimeSubset"]/input').send_keys(keys). a# q8 i, z. _. } h. h
driver.implicitly_wait(10). {6 v3 M! |+ x D6 g
# click to submit
3 x3 v9 {- r* V2 R) x% `, A driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[3]/td/input[1]').click()6 f* l0 k/ q" y" J$ y& T
#time.sleep(3)
0 [$ [( K/ a+ ^ T' N( V #driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[3]/td/input[1]').click()% P! _/ y! G2 W+ F% W/ j. k
time.sleep(10)
2 o4 b" g9 p( K" V. ]) ^ dir = '/Users/qiujingyi/Downloads/'
2 c1 x* a3 S( }4 Q" L% j& L oldname = '2012' + '.nc'- G6 p& K5 q/ @1 B
newname = '201201' + str(Day) + str(Hour) + '0000' + '.nc'
; `, H; b$ c0 v& R os.rename(os.path.join(dir,oldname),os.path.join(dir,newname))9 k3 w/ R9 d! G; ^) i4 j6 n' `4 [
'''
# c- |9 A, m. u! q( R" P
! x" B2 Y' c4 h- R3 ~* Y4 l/ F
) a: }' z; x8 b+ c. ? # X/ y5 U. H* q) C8 m5 R
5 P+ D, ^5 V1 m/ x- B
转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。 |