我尝试过请求、pydap、urllib和netcdf4,并在试图下载以下美国宇航局数据时不断获得重定向错误或权限错误:
GLDAS_NOAH025SUBP_3H: GLDAS诺亚陆面模型L4 3小时0.25 x 0.25度分段V001 (V001/summary?keywords=Hydrology)
我正在尝试下载大约50k文件,下面是一个示例,当粘贴到google chrome浏览器(如果您有正确的用户名和密码)时,它可以工作:
VERSION=001
有人有使用python从网络上获取OPeNDAP美国宇航局数据的经验吗?如果需要,我很乐意提供更多的信息。
以下是导致401错误的请求尝试:
import requests
def httpdownload():
'''loop through each line in the text file and open url'''
httpfile = open(pathlist[0]+"NASAdownloadSample.txt", "r")
for line in httpfile:
print line
outname = line[-134:-122]+".hdf"
print outname
username = ""
password = "*"
r = requests.get(line, auth=("username", "password"), stream=True)
print r.text
print r.status_code
with open(pathlist[0]+outname, 'wb') as out:
out.write(r.content)
print outname, "finished" # keep track of progress下面是提供重定向错误的pydap示例:
import install_cas_client
from pydap.client import open_url
def httpdownload():
'''loop through each line in the text file and open url'''
username = ""
password = ""
httpfile = open(pathlist[0]+"NASAdownloadSample.txt", "r")
fileone = httpfile.readline()
filetot = fileone[:7]+username+":"+password+"@"+fileone[7:]
print filetot
dataset = open_url(filetot)发布于 2016-10-18 14:11:44
我没有找到使用python的解决方案,但考虑到我现在掌握的信息,这应该是可能的。我在.netrc文件和cookie文件中使用wget,如下所示(https://disc.gsfc.nasa.gov/information/howto?title=How%20to%20Download%20Data%20Files%20from%20HTTP%20Service%20with%20wget):
#!/bin/bash
cd # path to output files
touch .netrc
echo "machine urs.earthdata.nasa.gov login <username> password <password>" >> .netrc
chmod 0600 .netrc
touch .urs_cookies
wget --content-disposition --trust-server-names --load-cookies ~/.urs_cookies --save-cookies ~/.urs_cookies --auth-no-challenge=on --keep-session-cookies
-i <path to text file of url list>希望它能帮助其他人从这台服务器处理NASA的数据。
发布于 2020-05-07 17:05:28
我意识到现在回答这个问题有点晚了,但是我在尝试做同样的事情时偶然发现了这个问题,所以我会把我的解决方案留在这里。看起来NASA服务器使用重定向和基本授权的方式是标准库所不期望的。当您从(例如) https://hydro1.gesdisc.eosdis.nasa.gov下载时,您将被重定向到https://urs.earthdata.nasa.gov进行身份验证。该服务器将身份验证令牌设置为cookie,并将您重定向回下载该文件。如果您没有正确地处理cookie,您将陷入无限重定向循环。如果您没有正确地处理身份验证和重定向,您将得到一个访问拒绝错误。
要解决这个问题,请将HTTPRedirectHandler、HTTPCookieProcessor和HTTPPasswordMgrWithDefaultRealm链接在一起,并将其设置为默认的打开器,或者直接使用该打开器。
from urllib import request
username = "<your username>"
password = "<your password>"
url = "<remote url of file>"
filename = "<local destination of file>"
redirectHandler = request.HTTPRedirectHandler()
cookieProcessor = request.HTTPCookieProcessor()
passwordManager = request.HTTPPasswordMgrWithDefaultRealm()
passwordManager.add_password(None, "https://urs.earthdata.nasa.gov", username, password)
authHandler = request.HTTPBasicAuthHandler(passwordManager)
opener = request.build_opener(redirectHandler,cookieProcessor,authHandler)
request.install_opener(opener)
request.urlretrieve(url,filename)https://stackoverflow.com/questions/40088745
复制相似问题