Python Requests

The first job is to install ‘requests’. Use pip to do this.

Creating the response object

For this, you will have to import to request module first.

Now, we will try to get a webpage. You can choose any webpage. For this example, I choose Google’s home page.

Here, ‘r’ is the response object. You can name it ‘response’ also or use any other name following the python variable naming conventions.

Response Methods

response.headers : Use this to get a dictionary of response headers

response.encoding : Use this to know the encoding used to decode the response.content

response.elapsed : This will give you a timedelta object with the time elapsed from sending the request to the arrival of the response

response.close() : This closes the server connection

response.content : You will get the content of the response in bytes

response.cookies : This returns a Cookiejar object with the cookies sent back from the server

response.history : This returns a list of response objects holding the history of request (url)

response.is_permanent_redirect : It will return ‘True’ is the response is the permanent redirected url, otherwise it will return ‘False’

response.is_redirect : It will return ‘True’ is the response was redirected, otherwise it will return ‘False’

response.iter_content() : It iterates over the response.content

response.json() : Returns a JSON object of the result (if the result was written in JSON format, if not it raises an error)

response.url() : Returns the URL of the response

response.text : Returns the content of the response in unicode

response.status_code : Returns a number that indicates the status (200 is Ok, 404 is Not Found)

response.request : Returns the request object that requested this response

response.reason : Returns a text corresponding to the status code

response.raise_for_status() : Returns an HTTPError object if an error has occurred during the process

response.ok : Returns ‘True’ if status_code is less than 200, otherwise ‘False’

response.links : Returns the header links

References

https://www.geeksforgeeks.org/python-requests-tutorial/

 

 

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *