如何解决尝试API实现时,Python代码中存在JSONDecodeError
我正试图打美国国家航空航天局api并获取地球图片。在python中实现时,出现以下错误:
** 跟踪(最近一次通话结束): 文件“ C:/Users/VC/AppData/Local/Programs/Python/Python38-32/REST/sample_APICall.py”,第13行 数据= response.json() 文件“ C:\ Users \ VC \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ site-packages \ requests \ models.py”,行898,位于json中 返回complexjson.loads(self.text, kwargs) 载入中的文件“ C:\ Users \ VC \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ json_ init _。py”,第357行 返回_default_decoder.decode(s) 文件“ C:\ Users \ VC \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ json \ decoder.py”,行337解码 obj,end = self.raw_decode(s,idx = _w(s,0).end()) raw_decode中的第355行的文件“ C:\ Users \ VC \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ json \ decoder.py” 从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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。