您的位置:首页 > 其它

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

2018-11-07 16:35 501 查看

爬虫笔记: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

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: