Python Requests Module Tutorial

28/12/2020
Requests is a popular apache2 licensed module in Python that can be used to interact with HTTP servers such as world wide web servers to download content that can be used for parsing websites or automatically posting to web forms. You can make a GET request, a POST request, passing parameters in URLs, get response content and addition of custom headers.

In this article, we’ll look at the Requests module in python and its basic operation with some examples and then finally we will conclude.

Installation

Python versions 2.6-2.7 and 3.3-3.6 supports the request module. Request is an external module so you have to install it by writing the following in your command prompt or terminal:

$ pipenv install requests

Before we move on you need to make sure of two things:

– Requests library is installed properly if not follow the link (http://docs.python-requests.org/en/master/user/install/#install)

-Requests library is up-to-date if not follow the link to check (http://docs.python-requests.org/en/master/community/updates/#updates)

GET and POST Requests

Start off by importing requests. Now we are going to try to get a webpage using get request.

Import requests
R_webpage = requests.get(‘http://www.dataversity.net/’)

R_webpage is a response object. All the information about the web page can be extracted from this object.

Now, if you want to make a post request:

import requests
R_post = requests.post(‘http://www.dataversity.net/’, data = {‘key’ : ‘value’})

See how easy it is to make requests. Let’s move on to passing parameters in URLs:

Passing parameters in URLs

Parameters in URLs can be passed in a formal way. Requests allow us to give these arguments as a dictionary of strings. params is the keyword to use in the arguments for that purpose.
See the following example to get a clear idea:

import requests
R_par = requests.get(‘http://www.dataversity.net, params = {‘key0’ : ‘value0’ ,
‘key1’ : ‘value1’})
print(R_par.url)

print statement helps identify if the URL has been encoded correctly.

Response Content

Response of the server can be viewed completely as text:

import requests
R_Content = requests.get(‘http://www.dataversity.net)
R_Content.text

The complete text will be decoded after getting it from the server and displayed as text.

Custom Headers

Custom headers can be added to requests. headers is the parameter which will have a dictionary passed by argument in order to specify the header.

Import requests
R_head = requests.get(‘http://www.dataversity.net, headers= {‘key’ : ‘value’})

In place of key and value, you can put your desired values throughout.

Conclusion

You were given a basic introduction of Python request module along with its working. Now, if you practice the above given examples on your own and add, eliminate and substitute things then you will get a better idea of its working. If you have made it here, congratulations because you have learned how to make basic requests to a server, passing parameters or arguments to the URLs, getting response content and showing it and passing custom headers. This will be very useful when you are trying to scrape webpages for information.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Python for Hacking, Build a Port Scanner

Python for Hacking: Build a Port scanner in 13 Lines With Python Becoming a penetration tester is not just merely being...
28/12/2020

Sending emails with Python

Sending, checking and replying to emails is a time consuming task, especially when you are doing it for a large number...
29/12/2020

How to Calculate Matrices in Python Without NumPy

For many applications, you need mathematics. In Python there is the maths module which handles the basics such as rounding,...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024