尝试API实现时Python代码中出现JSONDecodeError

尝试API实现时Python代码中出现JSONDecodeError,python,json,api,http,runtime-error,Python,Json,Api,Http,Runtime Error,我正在尝试点击美国宇航局的api,获取地球图片。在python中实现时,出现以下错误: **回溯(最近一次呼叫最后一次): 文件“C:/Users/VC/AppData/Local/Programs/Python/Python38-32/REST/sample_APICall.py”,第13行,在 data=response.json() json格式的文件“C:\Users\VC\AppData\Local\Programs\Python\Python38-32\lib\site packag

我正在尝试点击美国宇航局的api,获取地球图片。在python中实现时,出现以下错误:

**回溯(最近一次呼叫最后一次): 文件“C:/Users/VC/AppData/Local/Programs/Python/Python38-32/REST/sample_APICall.py”,第13行,在 data=response.json() json格式的文件“C:\Users\VC\AppData\Local\Programs\Python\Python38-32\lib\site packages\requests\models.py”,第898行 返回complexjson.load(self.text,kwargs) 文件“C:\Users\VC\AppData\Local\Programs\Python\Python38-32\lib\json\u init\uuz.py”,第357行,加载 返回\u默认\u解码器。解码 文件“C:\Users\VC\AppData\Local\Programs\Python38-32\lib\json\decoder.py”,第337行,在decode中 obj,end=self.raw\u decode(s,idx=\u w(s,0.end()) 文件“C:\Users\VC\AppData\Local\Programs\Python38-32\lib\json\decoder.py”,第355行,原始解码 从None引发JSONDecodeError(“预期值”,s,err.value) json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)

下面是代码。请帮助调试它

import requests
    import re
    import imageio
    from skimage import transform,io
    #get json with information about Earth
    payload = {}
    response = requests.post("https://api.nasa.gov/planetary/apodapi_key=QZJNMoj1n0qKWuiblxYFKbSYSGHLUdAu58hsd0U9",
    headers = {
      'x-api-key': 'QZJNMoj1n0qKWuiblxYFKbSYSGHLUdAu58hsd0U9'
     }
    )
    data = response.json()
    dates_pattern = r"^(?P<year>d{4})-(?P<month>d{2})-(?P<day>d{2})"
    for img in data['contextWrites']['to']:
      #get year,month and day with regex to create image URL
      matches = re.search(dates_pattern, img['date'])
      year = matches.group('year')
      month = matches.group('month')
      day = matches.group('day')
      image_name = img['image']
      img_url = f'https://epic.gsfc.nasa.gov/archive/natural/{year}/{month}/{day}/png/{image_name}.png'
      img_data = requests.get(img_url).content
      with open(f'images/{image_name}.png', 'wb') as handler:
         handler.write(img_data)
    index = range(len(data['contextWrites']['to']))
    images = []
    # resize images and create gif from them
    for i in index:
       img_name = data["contextWrites"]["to"][i]["image"]
       img = io.imread(f'images/{img_name}.png')
       small_img = transform.resize(img, (500, 500), mode='symmetric', preserve_range=True)
       images.append(small_img)
    imageio.mimsave('images/earth500.gif', images)
导入请求
进口稀土
导入图像
从skimage导入转换,io
#获取关于地球的json信息
有效载荷={}
响应=请求。发布(“https://api.nasa.gov/planetary/apodapi_key=QZJNMoj1n0qKWuiblxYFKbSYSGHLUdAu58hsd0U9",
标题={
“x-api-key”:“QZJNMoj1n0qKWuiblxYFKbSYSGHLUdAu58hsd0U9”
}
)
data=response.json()
日期模式=r“^(?Pd{4})-(Pd{2})-(Pd{2})”
对于数据['contextWrites']['to']中的img:
#使用正则表达式获取年、月和日以创建图像URL
匹配=重新搜索(日期模式,img['date'])
年份=匹配项。组(“年份”)
月份=匹配项。组(“月份”)
day=匹配项。组('day'))
image\u name=img['image']
img_url=f'https://epic.gsfc.nasa.gov/archive/natural/{year}/{month}/{day}/png/{image_name}.png'
img_data=requests.get(img_url).content
使用open(f'images/{image_name}.png,'wb')作为处理程序:
handler.write(img_数据)
索引=范围(len(数据['contextWrites']['to']))
图像=[]
#调整图像大小并从中创建gif
对于索引中的i:
img_name=data[“contextWrites”][“to”][i][“image”]
img=io.imread(f'images/{img_name}.png')
small\u img=transform.resize(img,(500500),mode='symmetric',preserve\u range=True)
images.append(小图片)
imageio.mimsave('images/earth500.gif',images)