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'>
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.
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__']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With