在Google Colaboratory上使用NASA数据的请求

2024-04-28 19:58:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Request从NASA数据访问中下载很多lik,但我得到了error code 401,可能是因为我无法进行身份验证。实际上,我有一个.netrc文件,其中包含代码machine urs.earthdata.nasa.gov login <uid> password <password>,但我不知道Google Colaboratory如何识别和运行允许下载文件的.netrc文件

我尝试使用的代码是:

  URL = rutas_GPM.iloc[i,0]
   
  # Set the FILENAME string to the data file name, the LABEL keyword value, or any customized name. 
  FILENAME = 'GPM{0}.nc'.format(i)
  result = requests.get(URL)
  try:
    result.raise_for_status()
    f = open("/content/drive/MyDrive/Colab Notebooks/Prueba/"+FILENAME,'wb')
    f.write(result.content)
    f.close()
    print('contents of URL written to '+FILENAME)
  except:
    print('requests.get() returned an error code '+str(result.status_code))

你能帮我解决这个问题吗

谢谢


Tags: 文件theto代码nameurlgetcode
1条回答
网友
1楼 · 发布于 2024-04-28 19:58:37

Google Colaboratory使用基于Linux的虚拟环境,按照此顺序,您必须遵循NASA针对MAC/Linux建议的说明:

cd $HOME
touch .netrc
echo "machine urs.earthdata.nasa.gov login <uid> password <password>" >> .netrc
chmod 0600 .netrc

<uid>是您的NASA ID,<password>是您的密码帐户

此外,您必须将以前在Windows操作系统中创建的.netrc文件上载到PC上的“home”和“root”文件夹(在colab环境中)。然后,运行代码并开始下载这些文件

相关问题 更多 >