对于python,它对<class'request.models.response'=“”>

时间:2017-11-09 04:06:58

标签: python class python-requests

我尝试在python中使用Requests库进行爬网,我首先导入了请求模块,然后我使用get函数调用网站获取名为r的响应,但我无法理解为什么r的类型是类你能告诉我为什么,非常感谢你。

我还想查看请求标题,我检查了一些文档,它说我可以使用r.request.headers,这里的请求是什么意思,它是一个方法吗?

>>> import requests
>>> r=requests.get("http://www.baidu.com")
>>> type(r)
<class 'requests.models.Response'>

3 个答案:

答案 0 :(得分:2)

启动请求后,您将获得Response个对象。要从响应对象获取数据,您需要访问您所追求的属性,例如: r.status_coder.text

有关详细信息,请参阅this documentation

答案 1 :(得分:0)

对于 具有以下属性:

['_content', '_content_consumed', '_next', 'status_code', 'headers', 'raw', 'url', 'encoding', 'history', 'reason', 'cookies', 'elapsed', 'request', 'connection', '__module__', '__doc__', '__attrs__', '__init__', '__enter__', '__exit__', '__getstate__', '__setstate__', '__repr__', '__bool__', '__nonzero__', '__iter__', 'ok', 'is_redirect', 'is_permanent_redirect', 'next', 'apparent_encoding', 'iter_content', 'iter_lines', 'content', 'text', 'json', 'links', 'raise_for_status', 'close', '__dict__', '__weakref__', '__hash__', '__str__', '__getattribute__', '__setattr__', '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__new__', '__reduce_ex__', '__reduce__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__dir__', '__class__']

答案 2 :(得分:-1)

这只是对您发送的请求的回应,希望对您有所帮助:)