首页 > 解决方案 > 处理来自 imageio.imread 的 HTTPError

问题描述

我正在使用imageio.imread并且有一个可以给出 404 错误的图像 URL 列表。

import imageio as io

try:
   imag = io.imread("http://static.booking.com/images/hotel/org/591/59160587.jpg")
except HTTPError as e:
   print("whatever")

这种或以下方法都不适合我:

使用 urllib

import imageio as io
from urllib3.exceptions import HTTPError as BaseHTTPError

try:
   imag = io.imread("http://static.booking.com/images/hotel/org/591/59160587.jpg")
except BaseHTTPError as e:
   print("whatever")

使用 requests.exceptions

import imageio as io
from requests.exceptions import HTTPError

try:
   imag = io.imread("http://static.booking.com/images/hotel/org/591/59160587.jpg")
except HTTPError as e:
   print("whatever")

下面的 Colab 笔记本显示了两者如何给出HTTPError: HTTP Error 404: Not Found https://colab.research.google.com/drive/1uOOzJ4jDvYKe5zdFfxDkbcNRpxcjaOOj

标签: python-3.xpython-imageio

解决方案


我用

import imageio as io
from urllib import error

try:
   imag = io.imread("http://static.booking.com/images/hotel/org/591/59160587.jpg")
except error.HTTPError as e:
   print("whatever")

推荐阅读