国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

對python requests的content和text方法的區別詳解

2020-02-15 23:08:39
字體:
來源:轉載
供稿:網友

問題:

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

看下源碼:

@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."""

結論是:

resp.text返回的是Unicode型的數據。

resp.content返回的是bytes型也就是二進制的數據。

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

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

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

舉個栗子

# 例如下載并保存一張圖片import requestsjpg_url = 'http://img2.niutuku.com/1312/0804/0804-niutuku.com-27840.jpg'content = requests.get(jpg_url).contentwith open('demo.jpg', 'wb') as fp:  fp.write(content)

以上這篇對python requests的content和text方法的區別詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 全南县| 罗甸县| 离岛区| 固镇县| 崇明县| 光山县| 襄汾县| 晴隆县| 安泽县| 霍山县| 闸北区| 景宁| 城步| 保康县| 永康市| 普兰县| 安陆市| 盘山县| 海晏县| 盈江县| 治县。| 房产| 扬州市| 开阳县| 连山| 深水埗区| 浑源县| 乌审旗| 化德县| 富裕县| 湘西| 麻城市| 商城县| 阳新县| 沛县| 武功县| 乐山市| 彭州市| 精河县| 舟曲县| 罗甸县|