python requests的content和text方法的区别
requests模块的content和text属性的区别,从print 结果来看是没有任何区别的。从源码描述信息上来看,可以发现resp.text返回的是Unicode型的数据、resp.content返回的是bytes型也就是二进制的数据。也就是说,如果你想取文本,可以通过r.text。如果想取图片,文件,则可以通过r.content。如果要取回json数据,可以使用resp.json()返回json格式。
content和text方法的描述信息
1 @property
2 def text(self):
3 """Content of the response, in unicode.
4 If Response.encoding is None, encoding will be guessed using
5 ``chardet``.
6 The encoding of the response content is determined based solely on HTTP
7 headers, following RFC 2616 to the letter. If you can take advantage of
8 non-HTTP knowledge to make a better guess at the encoding, you should
9 set ``r.encoding`` appropriately before accessing this property.
10 """
11 #content的完整代码就不贴了。
12 @property
13 def content(self):
14 """Content of the response, in bytes."""
测试示例
1import requests
2jpg_url = 'http://wiki.361way.com/static/logo.png'
3content = requests.get(jpg_url).content
4with open('demo.jpg', 'wb') as fp:
5 fp.write(content)
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/requests-content-text/6364.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.