使用python获取图像

2024-09-30 04:38:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个网站得到一个图像,我不知道我做错了什么。 这是我的代码:

import httplib2

h = httplib2.Http('.cache')

response, content = h.request('http://1.bp.blogspot.com/-KSBzFF0bIyU/TtFyj-KgC0I/AAAAAAAABEo/o43IoY_9Bec/s1600/praia-de-ponta-verde-maceio.jpg')

print(response.status)

with open('maceio.jpg', 'wb') as f:
    print(content, file = f)

--------------------------------------------------------------------------------

 200
Traceback (most recent call last):
  File "/home/matheus/workspace/Get Link/new_method_v2.py", line 12, in <module>
    print(content, file = f)
TypeError: 'str' does not support the buffer interface

Tags: 代码图像importhttpcache网站responserequest
1条回答
网友
1楼 · 发布于 2024-09-30 04:38:41

错误由以下行引起:

print(content, file = f)

print隐式地将名为contentbytes对象转换为字符串(str对象),该字符串不能以二进制模式写入文件,因为Python不知道要使用哪个字符编码。在

你为什么要绕道走print?只需使用file.write()方法将内容写入文件:

^{pr2}$

相关问题 更多 >

    热门问题