7

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'>
jing
  • 89
  • 1
  • 1
  • 3

4 Answers4

13

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.

Jerry
  • 1,127
  • 2
  • 17
  • 35
4

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__']
Hushen
  • 427
  • 1
  • 4
  • 15
1

It is response from HTTP request. If this is a REST API that you are making a call to, you can convert the response to JSON by calling r.json().

Huynh Triet
  • 81
  • 3
  • 3
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30749405) – Simas Joneliunas Jan 09 '22 at 10:20
-1

Its just a response to the request you're sending, hope this helps :)

Peacock
  • 1
  • 1