Python AttributeError: 'Response' 对象没有属性 'text'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22689334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:24:59  来源:igfitidea点击:

AttributeError: 'Response' object has no attribute 'text'

pythonpython-requests

提问by user3438081

page = requests.get(url)
tree = html.fromstring(page.text)

At line 2 I get:

在第 2 行,我得到:

AttributeError: 'Response' object has no attribute 'text'

I have imported requests and installed requests. How do I debug this error? can anyone tell command to install lastest requests module?

我已经导入了请求并安装了请求。我如何调试这个错误?谁能告诉命令安装最新的请求模块?

采纳答案by Martijn Pieters

You are using an ancient version of requests, most likely because it came installed with Ubuntu Precise Pengolin (12.04LTS).

您正在使用 的古老版本requests,很可能是因为它随Ubuntu Precise Pengolin (12.04LTS)一起安装。

You'll either have to refer to the version specific documentationor use a more recent version. requestshas changed drastically since the 0.x version days.

您要么必须参考特定版本的文档,要么使用更新的版本。requests自 0.x 版本以来发生了巨大变化。

For your old version, if you set response.encoding, response.contentwill contain a decoded Unicode value.

对于您的旧版本,如果您设置response.encoding,response.content将包含一个解码的 Unicode 值。