爬虫笔记:response.text和response.content的区别

爬虫笔记:response.text和response.content的区别

text 返回的是unicode 型的数据,一般是在网页的header中定义的编码形式

>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.text
# u'[{"repository":{"open_issues":0,"url":"https://github.com/...

content返回的是bytes,二级制型的数据。


# 例如下载并保存一张图片
 
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:

大白话

如果想要提取文本就用text

但是如果你想要提取图片、文件,就要用到content

猜你喜欢

转载自blog.csdn.net/qinglianchen0851/article/details/83826565