我试图从一个网站得到一个图像,我不知道我做错了什么。 这是我的代码:
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
错误由以下行引起:
print
隐式地将名为content
的bytes
对象转换为字符串(str
对象),该字符串不能以二进制模式写入文件,因为Python不知道要使用哪个字符编码。在你为什么要绕道走
^{pr2}$print
?只需使用file.write()
方法将内容写入文件:相关问题 更多 >
编程相关推荐