Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For python, what does it mean for <class 'requests.models.Response'>

I tries to use Requests library in python for crawling, I first imported the requests module,and then I use get function to call the website for getting a response named r, but I can not understand why the type of r is class,could you please tell me why ,thank you very much.

I also want to check the request header, I checked some documents, it says that I can use r.request.headers, what does the request here mean, is it a method?

>>> import requests
>>> r=requests.get("http://www.baidu.com")
>>> type(r)
<class 'requests.models.Response'>
like image 445
jing Avatar asked Jan 04 '23 05:01

jing


2 Answers

You're getting a Response object after you fire off the request. To get data from the response object you need to access the property you're after, e.g. r.status_code, r.text, etc.

See this documentation for more details.

like image 108
Jerry Avatar answered Jan 05 '23 18:01

Jerry


For the <class 'requests.models.Response'> has following attributes:

['_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__']
like image 20
Hushen Avatar answered Jan 05 '23 20:01

Hushen