使用 Python 请求从 URL 保存图像 - URL 类型错误 [英] Saving Image from URL using Python Requests - URL type error

查看:76
本文介绍了使用 Python 请求从 URL 保存图像 - URL 类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码:

    with open('newim','wb') as f:
        f.write(requests.get(repr(url)))

网址在哪里:

<代码> URL = '数据:图像/PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAFoAAAArCAYAAAD41p9mAAAAzUlEQVR42u3awQ4DIQhFUf7/p9tNt20nHQGl5yUuh4c36BglgoiIiIiIiGiVHq + RGfvdiGG + lxKonGiWd4vvKZNd5V/u2zXRO953c2jx3bGiMrewLt + PgbJA/xJ3RS5dvl9PEdXLduK3baeOrKrc1bcF9MnLP7WqgR4GOjtOl28L6AlHtLSqBhpooIEGGmiggQYaaKCBBhpodx3H3XW4vQN6HugILyztoL0Zhlfw9G4tfR0FfR0VnTw6lQoT0XtXmMxfdJPuALr0x5Pp + wT35KKWb6NaVgAAAABJRU5ErkJggg =='

我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python33\lib\site-packages\requests\api.py", line 69, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Python33\lib\site-packages\requests\api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "C:\Python33\lib\site-packages\requests\sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python33\lib\site-packages\requests\sessions.py", line 567, in send
    adapter = self.get_adapter(url=request.url)
  File "C:\Python33\lib\site-packages\requests\sessions.py", line 641, in get_adapter
    raise InvalidSchema("No connection adapters were found for '%s'" % url)

我看过其他帖子,乍一看似乎是一个类似的问题,但我只是添加https://"或类似的东西我没有任何运气......我真的想避免在 webdriver+Autoit 或其他东西中执行此操作,因为我必须对数千张图像进行类似的练习.

I have seen other posts with what, at first glance, appears to be a similar problem but I haven't had any luck just adding 'https://' or anything like that...I seriously want to avoid having to do this in webdriver+Autoit or something because I have to do a similar exercise for thousands of images.

推荐答案

这是一张以 base64 编码的图像.引用下面的 URL:base64 等于图像本身的文本(字符串)表示".

This is an image encoded in base64. Quoting the URL below: "base64 equals to text (string) representation of the image itself".

阅读此内容以获取详细说明:http://www.stoimen.com/blog/2009/04/23/when-you-should-use-base64-for-images/

Read this for a detailed explanation: http://www.stoimen.com/blog/2009/04/23/when-you-should-use-base64-for-images/

为了使用它们,您必须实现一个 base64 解码器.幸运的是,SO 已经为您提供了如何去做的答案:

In order to use them you'll have to implement a base64 decoder. Luckily SO already provides you with the answer on how to do it:

Python base64 数据解码

这篇关于使用 Python 请求从 URL 保存图像 - URL 类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆