而不是urllib
您可以使用requests
模塊,使下載內容多更容易。實際工作的部分只有4條線。
# first install this module
import requests
# parameters to change
location = {
'part': 'USWC',
'part2': '_US_West_Coast',
'km': '6km',
'north': '45.0000',
'west': '-120.0000',
'east': '-119.5000',
'south': '44.5000',
'start': '2016-10-01',
'end': '2016-10-02'
}
# this is template for .format() method to generate links (very naive method)
link_template = "http://hfrnet.ucsd.edu/thredds/ncss/grid/HFR/{part}/{km}/hourly/RTV/\
HFRADAR,{part2},_{km}_Resolution,_Hourly_RTV_best.ncd?var=u&var=v&\
north={north}&west={west}&east={east}&south={south}&horizStride=1&\
time_start={start}T00:00:00Z&time_end={end}T16:00:00Z&timeStride=1&addLatLon=true&accept=netcdf"
# some debug info
link = link_template.format(**location)
file_name = location['part'] + location['km'] + location['start'].replace('-', '') + '-' + location['end'].replace('-', '')
print("Link: ", link)
print("Filename: ", file_name)
# try to open webpage
response = requests.get(link)
if response.ok:
# open file for writing in binary mode
with open(file_name, mode='wb') as file_out:
# write response to file
file_out.write(response.content)
可能下一步將在包含位置字典的列表中循環運行此循環。或者可能從csv文件中讀取位置。
來源
2016-11-08 19:47:21
mx0
要打開瀏覽器窗口,其中的鏈接或下載任何與該鏈接點保存到? – mx0
根據您提供的規格,該鏈接會自動開始下載數據。所以基本上我想把它作爲程序中的一個基礎鏈接,並且能夠更改粗體部分,這樣我就可以獲得不同的數據集。用戶只需在鏈接中輸入北,西,東,南數字和其他粗體部分,即可開始下載。 – EagleTamer