>>>利用Python库实现HYCOM海洋数据批量下载(备注:Chromedriver要对应浏览器版本号!)
5 q% _) B i8 z; d$ R2 T4 D$ M4 y9 w=====================================================================! V0 f: h8 a2 Q
import time
1 f6 L. J" R2 V8 F# Iimport os! ]& \, Y) A6 r2 a
from selenium import webdriver
( b7 D) g3 c2 cfrom selenium.webdriver.support.select import Select
9 T6 Y/ L9 |7 l0 f: ]4 bdriver = webdriver.Chrome('/Library/Frameworks/Python.framework/Versions/3.9/bin/chromedriver')5 t2 J+ A k6 L
driver.get('www.52ocean.cn')
) m# T8 d% j( ~" L#driver.get('www.52ocean.cn')
4 c- L. W) {9 F2 xdriver.implicitly_wait(10)
! Y" F, g1 Z, u0 _# click ele/ j: J9 v& ?% t) B- f- J+ i
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[1]').click()# H: g8 s% |6 ~2 j5 [! Z
# click S,T,U,V4 q% Y+ h/ c' h0 Z$ C
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[2]').click()0 P9 Z( Z9 q* a4 D6 O
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[3]').click()0 d% H# p" {2 |: s. Q* c
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[4]').click()8 {1 m& R; k0 I- G# k( k
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[1]/blockquote/input[5]').click()- w" c! [( P' H) b* y$ I
driver.implicitly_wait(5)
7 h8 W4 l# M; G; V# click Disable horizontal subsetting
) Z: ^( A) K% ?- `5 |driver.find_element_by_xpath('//*[@id="disableLLSubset"]').click()
% l |' ?3 ]) ^7 }# input lat,lon
! J9 I7 V5 K6 h' }6 @3 d/ Jdriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[1]/input[1]').click()# t5 B" R+ f" r( }. b
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[1]/input[1]').clear()
+ N! N2 N$ J0 w2 jdriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[1]/input[1]').send_keys('2')9 \0 K/ F# _7 z' w+ G/ {
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[1]').click()
2 ~3 V* W; \4 l( N% t2 fdriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[1]').clear()
' z, `6 Y: P9 r" S3 pdriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[1]').send_keys('84')
; q& ]( ]$ b: cdriver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[3]').click()* M- G+ B- g" M# m
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[3]').clear()' L l$ ?) h5 M! U7 @0 G1 v. v
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[2]/input[3]').send_keys('94')8 g6 i; d Z% {
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[3]/input[1]').click()
( X) Q2 ^) B& D, W- C7 T5 ]driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[3]/input[1]').clear()* O/ B/ M# @/ l
driver.find_element_by_xpath('//*[@id="latlonSubset"]/div[3]/input[1]').send_keys('-2')
2 ^" s! g$ C3 D0 H! u) Q: n/ t9 Ndriver.implicitly_wait(1)
6 M& ^9 r( m8 O9 o' i$ r# click vertical stride
( j ^* N W( l) L4 o0 _5 Xdriver.find_element_by_xpath('//*[@id="inputVerticalStride"]/span').click()" X! F0 L, R% p4 g# r1 M* I8 A: d
driver.implicitly_wait(1)5 P8 R e) F/ O0 z# j
# click to add lon/lat variables- X/ Y0 D. J3 F/ @/ ?0 K' y
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[1]/td[2]/div[12]/input').click()8 T" a- r- H; p$ w4 x4 O" r, R% p
# choose output format
& O2 G, e* V$ Z8 ^- G+ fs = driver.find_element_by_name('accept')" \1 C. ^) G9 E
Select(s).select_by_value('netcdf')4 P: r4 `8 Q! F) i- V" r
# click single time, and input data_time" S+ l9 J f, Q9 L
# 天
* p; x/ F$ Q/ o8 H! I( W' ^' w4 qn = 07 @+ P5 n* Q- y8 t, \6 `2 y: o
for m in range(3,12+1): #月份
' e% u4 J+ s+ } `: `6 p# A if m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12:
) H/ K0 ~2 ^2 b% W' A& y month_num = 31 # 天数
0 o, E' o: ^7 j9 D0 a elif m == 4 or m == 6 or m == 9 or m == 11:
4 I3 J" D6 ^! h+ t# q9 L month_num = 30 # 天数' J6 Q5 j: d7 `
elif m == 2:' |4 |: _( w% w! f5 H, @% Y$ F8 z
month_num == 28 # 天数
/ P7 {* `* A- @8 Q: {, {& B6 U month = str(m).zfill(2)6 C/ b9 J* a) [0 X$ [: v$ J$ w6 W \
for i in range(1, month_num+1):, T( y7 P9 t! v4 k
Day = str(i).zfill(2)
/ K2 G7 h- f8 y7 Q5 h% j3 N keys = '2011-' + month + '-' + Day + 'T00:00:00Z' # 日期3 S$ F q5 e# v5 t
print(keys); M U! }, r$ V% y
driver.find_element_by_xpath('//*[@id="singleTimeSubset"]/input').clear(). j% c% y l, `. k
driver.find_element_by_xpath('//*[@id="singleTimeSubset"]/input').send_keys(keys)
' D$ }) b; O" T3 } driver.implicitly_wait(10)4 B6 ]/ L3 p$ R! n4 Z+ I+ X
# click to submit
- u% K* o0 q4 A; U# } driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[3]/td/input[1]').click()
5 t2 O' a1 }1 b( P #time.sleep(3)5 r& [1 i3 @1 r0 ~( ^# f- P8 X- m
#driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[3]/td/input[1]').click()6 v% W# {5 a, B- p# [5 ]: f
time.sleep(20)+ q2 `0 ~: ?2 G7 t0 }* O2 @* ?0 }
dir = '/Users/qiujingyi/Downloads/'
4 g3 w/ M! ], N) S7 Z oldname = '2011' + '.nc'
7 a' g, _7 |( ^: `5 [ newname = '2011' + str(month) + str(Day) + '000000' + '.nc'
2 q+ g$ n% a) z* q# k4 J os.rename(os.path.join(dir,oldname),os.path.join(dir,newname))
" D+ R) G& Y, ~1 q6 g'''; Q: v$ K1 Y! M% B' M$ e. I6 I
# 天+小时" g9 z& h" f9 f7 W' |
n = 0
3 G% z* F, U8 M( cfor i in range(1, 31):( w$ Z& m( Q$ x5 ], `1 f
Day = str(i).zfill(2)
5 D0 e2 o! G( x for j in range(0,21,3):% G% d" `. e; [$ c# J. F
Hour = str(j).zfill(2)
* k: U3 z( \* N& M: t* h6 w keys = '2012-01-' + Day + 'T' + Hour + ':00:00Z'( H$ ^8 M( [$ s0 a* Y
print(keys)- O, P1 a" z7 ?1 } P/ `
driver.find_element_by_xpath('//*[@id="singleTimeSubset"]/input').clear()
+ i0 w$ z: | e driver.find_element_by_xpath('//*[@id="singleTimeSubset"]/input').send_keys(keys)
\2 ~8 P x/ i: w/ P driver.implicitly_wait(10)
! Q" b2 X9 ~$ \" b # click to submit# w9 z* A5 x* m% o6 `, K) r
driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[3]/td/input[1]').click()
4 l; I3 |9 O' c* x# X& | #time.sleep(3)
) q" k5 k& A) e #driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[3]/td/input[1]').click(); t+ R; ^# u7 {5 b. p
time.sleep(10)
2 V/ L: ~7 o+ g1 y6 y0 s7 t dir = '/Users/qiujingyi/Downloads/'! C. s N. C- s. G' @" d8 H+ `! d( {
oldname = '2012' + '.nc'
- B$ W' w& s# I2 {1 S newname = '201201' + str(Day) + str(Hour) + '0000' + '.nc'
( [% b, c! g/ B os.rename(os.path.join(dir,oldname),os.path.join(dir,newname))
/ A2 ^2 J0 ?# c2 v6 |. N% ?'''
& e+ q( o4 {6 f( V. _$ X3 h+ E( i+ [# T
( |4 [" [7 `2 ^
q. [; l c. W% X, q0 Z' Y/ b% w, B
. |3 K9 |9 ?% U0 O' f# s 转载本文请联系原作者获取授权,同时请注明本文来自叶瑞杰科学网博客。 |