import os
import requests
class SessionHeaderRedirection(requests.Session):
AUTH_HOST = 'urs.earthdata.nasa.gov'
def __init__(self, username, password):
super().__init__()
self.auth = (username, password)
def rebuild_auth(self, prepared_request, response):
headers = prepared_request.headers
url = prepared_request.url
if 'Authorization' in headers:
original_parsed = requests.utils.urlparse(response.request.url)
redirect_parsed = requests.utils.urlparse(url)
if (original_parsed.hostname != redirect_parsed.hostname) and \
redirect_parsed.hostname != self.AUTH_HOST and \
original_parsed.hostname != self.AUTH_HOST:
del headers['Authorization']
return
class downloadHDF5:
def __init__(self, stageroot, username="****", password="****"):
self.stageroot = stageroot
self.username = username
self.password = password
self.session = SessionWithHeaderRedirection(self.username, self.password)
def download(self, url):
"""
urs.earthdata.nasa.gov 网站文件下载函数
:param url: 文件的url地址
:param stageroot: 存储下载文件的根目录的位置
:param username: 下载使用的用户名
:param password: 下载使用的密码
:return: 无
"""
filename = url[url.rfind('/') + 1:].split('?')[0]
try:
response = self.session.get(url, stream=True)
print(filename + ":" + str(response.status_code))
response.raise_for_status()
with open(os.path.join(self.stageroot, filename), 'wb') as fd:
for chunk in response.iter_content(chunk_size=1024 * 1024):
fd.write(chunk)
except Exception as e:
print("download" + filename + ":error")
print("\n")
print(e)
a = downloadHDF5('*/GPM_NC')
with open('*/subset_GPM_3IMERGHH_06_20220429_102358.txt') as f:
for url in f:
b = a.download(url)
Python下载GPM卫星数据
最新推荐文章于 2024-07-30 15:54:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)