Python requests GET method
In this article, you will learn about Python requests GET method.
The GET is one of the most common HTTP methods and used to request data from a specified resource. Python provides several HTTP libraries to handle GET and POST requests, like -
- httplib
- urllib
- requests
Python requests module
In this article, we are using the Python requests module.
The requests module makes a HTTP request to the specified web page using Python and returns a response object. The installation process of this package is similar to the other Python packages. We can either download this from GitHub or install it using pip tool.
$ pip install requests
Python get() method
Python requests module provides get() method to return data of the requested URL. It has the following syntax -
requests.get(url, params={key: value})
Here, url is the url of the request and required parameter. The params is optional parameter. Some of the optional parameters are auth, cert, cookies, headers, timeout, verify, etc.
Suppose we want to get HTML content of a website, the code will be as follows -
import requests
URL = 'https://www.google.com'
page = requests.get(URL)
print(page.content)
The above code returns the HTML content of the requested web page.
Get HTTP Status Code
The requests get method has 'status_code' attribute to get the current status code of the URL. The following code returns the status code of the specified url -
import requests
URL = 'https://www.google.com'
page = requests.get(URL)
print(page.status_code)
The above code returns the following output -
(env) c:\python37\Scripts\projects>webscrap.py
{'Roll': 1001, 'address': {'city': 'New York', 'postal code': '10021', 'state': 'NY', 'street address': '11 1nd Street'}, 'class': 10, 'firstname': 'Priska', 'lastname': 'Kashyap', 'section': 'A'}
Get HTTP Headers
The HTTP Headers make the client and server pass the additional information with a HTTP request and response. It consists of its case-insensitive name followed by a colon(:) and then by its value.
import requests
r = requests.get('https://www.google.com')
print(r.headers)
The above code returns output something like this -
HTTP Authentication
We can use the Authentication header to pass credentials to the server. This can be possible with the auth parameter of the get method.
import requests
url = 'https://www.etutorialspoint.com/authpage.php'
x = requests.get(url, auth = ('username', 'password'))
print(x.status_code)
Related Articles
Python requests GET methodHow to convert MySQL query result to JSON in Python
How to fetch data from mongodb using Python
CRUD operations in Python using MongoDB connector
Write Python Pandas Dataframe to CSV
Quick Introduction to Python Pandas
Python Pandas DataFrame
Python3 Tkinter Messagebox
Python get visitor information by IP address
Python Webbrowser
Python Tkinter Overview and Examples
Python Turtle Graphics Overview
Factorial Program in Python
Python snake game code with Pygame
Python JSON Tutorial - Create, Read, Parse JSON file
Python convert xml to dict
Python convert dict to xml