美文网首页
(Python)requests中content和text的区别

(Python)requests中content和text的区别

作者: 冰西瓜大郎 | 来源:发表于2017-04-24 15:29 被阅读0次
response = requests.get(url)
response.content
response.text
一直在想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.
        """

  
    @property
    def content(self):
        """Content of the response, in bytes."""
resp.text返回的是Unicode型的数据。
resp.content返回的是bytes型也就是二进制的数据

文本类型用text,图片、文件类型用contexnt

例子:保存抽屉的图片

import re
import requests

session = requests.session()
index = 1
try:
    f = open('chouti.txt','r')
    txt = f.read()
    pattern = re.compile('http://(.*?).jpg',re.S)
    items = re.findall(pattern, txt)
    for item in items:
        url = "http://" + item + ".jpg"
        respone = session.get(url)
        f1 = open(str(index)+".jpg", 'wb')
        f1.write(respone.content)
        f1.close()
        index+=1
#context open文件要用wb,text open文件用w。
finally:
    f.close()

相关文章

网友评论

      本文标题:(Python)requests中content和text的区别

      本文链接:https://www.haomeiwen.com/subject/mrdpzttx.html