无法从 API 检索图像 - SSL 证书错误
Can't retrieve image from API - SSL certificate error
我正在尝试从 Nasa 的 APOD API 获取图像并将其保存到我的计算机,但是在发送请求时,我收到 SSL 证书错误。我不确定如何解决它,因为我以前从未处理过这个问题。
这是我得到的:
raise SSLError(e)
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1124)
这是我的代码:
import nasapy
import os
from datetime import datetime
import urllib.request
from IPython.display import Image,display,Audio
import ssl
def nasa_apod(ctx):
key = "#####################"
nasa = nasapy.Nasa(key=key)
dtm = datetime.today().strftime("%Y-%m-%d")
apod = nasa.picture_of_the_day(date=dtm, hd=True)
print(apod)
if apod["media_type"] == "image":
title = dtm + "_" + apod["title"].replace(" ", "_").replace(":", "_") + ".jpg"
image_dir = "Astro_images"
dir_res = os.path.exists(image_dir)
if dir_res == False:
os.makedirs(image_dir)
else:
print("Directory already exists! ")
filename =os.path.join(image_dir, title)
with urllib.request.urlopen(url=apod["hdurl"], context=ctx) as u, \
open(filename, 'wb') as f:
f.write(u.read())
if ("date" in apod.keys()):
print("Date image released: ", apod["date"])
print("\n")
if ("copyright" in apod.keys()):
print("This image is owned by: ", apod["copyright"])
print("\n")
if ("title" in apod.keys()):
print("Title of the image: ", apod["title"])
print("\n")
if ("explanation" in apod.keys()):
print("Description for the image: ", apod["explanation"])
print("\n")
if ("hdurl" in apod.keys()):
print("URL for this image: ", apod["hdurl"])
print("\n")
display(Image(os.path.join(image_dir, title)))
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
nasa_apod(ctx)
出于安全原因,我隐藏了密钥。
感谢任何帮助!
您需要将 urllib.request.urlretrieve
替换为 urllib.request.urlopen
并在调用 nasa_apod
方法之前确定自定义 ssl 上下文,那么您的程序应该看起来像这样:
def nasa_apod(ctx):
key = "#####"
nasa = nasapy.Nasa(key=key)
dtm = datetime.today().strftime("%Y-%m-%d")
apod = nasa.picture_of_the_day(date=dtm, hd=True)
print(apod)
if apod["media_type"] == "image":
title = dtm + "_" + apod["title"].replace(" ", "_").replace(":", "_") + ".jpg"
image_dir = "Astro_images"
dir_res = os.path.exists(image_dir)
if dir_res == False:
os.makedirs(image_dir)
else:
print("Directory already exists! ")
filename=os.path.join(image_dir, title)
with urllib.request.urlopen(url=apod["hdurl"], context=ctx) as u, \
open(file_name, 'wb') as f:
f.write(u.read())
if ("date" in apod.keys()):
print("Date image released: ", apod["date"])
print("\n")
if ("copyright" in apod.keys()):
print("This image is owned by: ", apod["copyright"])
print("\n")
if ("title" in apod.keys()):
print("Title of the image: ", apod["title"])
print("\n")
if ("explanation" in apod.keys()):
print("Description for the image: ", apod["explanation"])
print("\n")
if ("hdurl" in apod.keys()):
print("URL for this image: ", apod["hdurl"])
print("\n")
display(Image(os.path.join(image_dir, title)))
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
nasa_apod(ctx)
我正在尝试从 Nasa 的 APOD API 获取图像并将其保存到我的计算机,但是在发送请求时,我收到 SSL 证书错误。我不确定如何解决它,因为我以前从未处理过这个问题。
这是我得到的:
raise SSLError(e)
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1124)
这是我的代码:
import nasapy
import os
from datetime import datetime
import urllib.request
from IPython.display import Image,display,Audio
import ssl
def nasa_apod(ctx):
key = "#####################"
nasa = nasapy.Nasa(key=key)
dtm = datetime.today().strftime("%Y-%m-%d")
apod = nasa.picture_of_the_day(date=dtm, hd=True)
print(apod)
if apod["media_type"] == "image":
title = dtm + "_" + apod["title"].replace(" ", "_").replace(":", "_") + ".jpg"
image_dir = "Astro_images"
dir_res = os.path.exists(image_dir)
if dir_res == False:
os.makedirs(image_dir)
else:
print("Directory already exists! ")
filename =os.path.join(image_dir, title)
with urllib.request.urlopen(url=apod["hdurl"], context=ctx) as u, \
open(filename, 'wb') as f:
f.write(u.read())
if ("date" in apod.keys()):
print("Date image released: ", apod["date"])
print("\n")
if ("copyright" in apod.keys()):
print("This image is owned by: ", apod["copyright"])
print("\n")
if ("title" in apod.keys()):
print("Title of the image: ", apod["title"])
print("\n")
if ("explanation" in apod.keys()):
print("Description for the image: ", apod["explanation"])
print("\n")
if ("hdurl" in apod.keys()):
print("URL for this image: ", apod["hdurl"])
print("\n")
display(Image(os.path.join(image_dir, title)))
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
nasa_apod(ctx)
出于安全原因,我隐藏了密钥。
感谢任何帮助!
您需要将 urllib.request.urlretrieve
替换为 urllib.request.urlopen
并在调用 nasa_apod
方法之前确定自定义 ssl 上下文,那么您的程序应该看起来像这样:
def nasa_apod(ctx):
key = "#####"
nasa = nasapy.Nasa(key=key)
dtm = datetime.today().strftime("%Y-%m-%d")
apod = nasa.picture_of_the_day(date=dtm, hd=True)
print(apod)
if apod["media_type"] == "image":
title = dtm + "_" + apod["title"].replace(" ", "_").replace(":", "_") + ".jpg"
image_dir = "Astro_images"
dir_res = os.path.exists(image_dir)
if dir_res == False:
os.makedirs(image_dir)
else:
print("Directory already exists! ")
filename=os.path.join(image_dir, title)
with urllib.request.urlopen(url=apod["hdurl"], context=ctx) as u, \
open(file_name, 'wb') as f:
f.write(u.read())
if ("date" in apod.keys()):
print("Date image released: ", apod["date"])
print("\n")
if ("copyright" in apod.keys()):
print("This image is owned by: ", apod["copyright"])
print("\n")
if ("title" in apod.keys()):
print("Title of the image: ", apod["title"])
print("\n")
if ("explanation" in apod.keys()):
print("Description for the image: ", apod["explanation"])
print("\n")
if ("hdurl" in apod.keys()):
print("URL for this image: ", apod["hdurl"])
print("\n")
display(Image(os.path.join(image_dir, title)))
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
nasa_apod(ctx)