最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

對python requests的content和text方法的區(qū)別詳解

 更新時(shí)間:2018年10月11日 10:33:49   作者:Op小劍  
今天小編就為大家分享一篇對python requests的content和text方法的區(qū)別詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

問題:

一直在想requests的content和text屬性的區(qū)別,從print 結(jié)果來看是沒有任何區(qū)別的

看下源碼:

@property
  def text(self):
    """Content of the response, in unicode.

    If Response.encoding is None, encoding will be guessed using
    ``chardet``.

    The encoding of the response content is determined based solely on HTTP
    headers, following RFC 2616 to the letter. If you can take advantage of
    non-HTTP knowledge to make a better guess at the encoding, you should
    set ``r.encoding`` appropriately before accessing this property.
    """

  #content的完整代碼就不貼了。
  @property
  def content(self):
    """Content of the response, in bytes."""

結(jié)論是:

resp.text返回的是Unicode型的數(shù)據(jù)。

resp.content返回的是bytes型也就是二進(jìn)制的數(shù)據(jù)。

也就是說,如果你想取文本,可以通過r.text。

如果想取圖片,文件,則可以通過r.content。

(resp.json()返回的是json格式數(shù)據(jù))

舉個(gè)栗子

# 例如下載并保存一張圖片

import requests

jpg_url = 'http://img2.niutuku.com/1312/0804/0804-niutuku.com-27840.jpg'

content = requests.get(jpg_url).content

with open('demo.jpg', 'wb') as fp:
  fp.write(content)

以上這篇對python requests的content和text方法的區(qū)別詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

杂多县| 西贡区| 齐齐哈尔市| 台北县| 邻水| 华亭县| 吴忠市| 青川县| 双鸭山市| 乐山市| 台湾省| 蓬安县| 繁昌县| 莫力| 松溪县| 璧山县| 镇安县| 潞西市| 石阡县| 林甸县| 凌海市| 安溪县| 旺苍县| 双峰县| 永新县| 苍山县| 防城港市| 靖州| 高碑店市| 图们市| 天台县| 镇赉县| 堆龙德庆县| 卢龙县| 色达县| 开化县| 怀远县| 丁青县| 宝应县| 洛扎县| 中阳县|