Đây là mã có liên quan để thực hiện cuộc gọi nhận HTTP từ tài liệu chính thức
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get['//httpbin.org/get', params=payload]
Để thích ứng với yêu cầu cụ thể của bạn:
import requests
payload = {'q': 'food'}
r = requests.get['//httpbin.org/get', params=payload]
print [r.text]
Đây là kết quả thu được nếu tôi chạy ví dụ thứ 2:
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
Mô -đun yêu cầu
Thí dụ
Thực hiện yêu cầu vào trang web và trả lại mã trạng thái:
Nhập yêu cầu
x = requests.get ['//w3schools.com'] in [x.status_code]
print[x.status_code]
Chạy ví dụ »
Định nghĩa và cách sử dụng
Phương thức
import requests
payload = {'q': 'food'}
r = requests.get['//httpbin.org/get', params=payload]
print [r.text]
1 gửi yêu cầu nhận đến URL được chỉ định.Cú pháp
requests.get [url, params = {key: value}, args]
Args có nghĩa là 0 hoặc nhiều hơn các đối số được đặt tên trong bảng tham số bên dưới. Thí dụ:
Yêu cầu.get [URL, thời gian chờ = 2.50]
Giá trị tham số
URL | Thử nó | Yêu cầu. URL của yêu cầu |
params | Thử nó | Yêu cầu. URL của yêu cầu Default 2
|
params | Thử nó | Yêu cầu. URL của yêu cầu Default 3 [allowing redirects]
|
params | Thử nó | Yêu cầu. URL của yêu cầu Default 2
|
params | Thử nó | Yêu cầu. URL của yêu cầu Default 2
|
params | Thử nó | Yêu cầu. URL của yêu cầu Default 2
|
params | Thử nó | Yêu cầu. URL của yêu cầu Default 2
|
params | Thử nó | Yêu cầu. URL của yêu cầu Default 2
|
params | Thử nó | Yêu cầu. URL của yêu cầu Default 9
|
params | Thử nó | Yêu cầu. URL của yêu cầu Default 2 which means the request will continue until the connection is closed
|
params | Không bắt buộc. Một từ điển, danh sách các bộ dữ liệu hoặc byte để gửi dưới dạng chuỗi truy vấn.default 2Try it | cho phép_redirects Default 3
|
Không bắt buộc. Một boolean để bật/tắt chuyển hướng.default import requests
payload = {'q': 'food'}
r = requests.get['//httpbin.org/get', params=payload]
print [r.text]
3 [cho phép chuyển hướng]
import requests
payload = {'q': 'food'}
r = requests.get['//httpbin.org/get', params=payload]
print [r.text]
Auth
Mô -đun yêu cầu
Bài đăng này thảo luận về hai phương thức yêu cầu HTTP [Supertext Transfer Transfer] & NBSP; Nhận và đăng các yêu cầu trong & nbsp; Python và việc triển khai của chúng trong Python.
HTTP là gì? HTTP là một tập hợp các giao thức được thiết kế để cho phép giao tiếp giữa máy khách và máy chủ. Nó hoạt động như một giao thức phản hồi yêu cầu giữa máy khách và máy chủ. Trình duyệt web có thể là máy khách và một ứng dụng trên máy tính lưu trữ trang web có thể là máy chủ.
HTTP is a set of protocols designed to enable communication between clients and servers. It works as a request-response protocol between a client and server.
A web browser may be the client, and an application on a computer that hosts a website may be the server.
Vì vậy, để yêu cầu phản hồi từ máy chủ, chủ yếu có hai phương pháp:
- Nhận: Để yêu cầu dữ liệu từ máy chủ. : to request data from the server.
- Đăng: Để gửi dữ liệu để được xử lý cho máy chủ. : to submit data to be processed to the server.
Dưới đây là một sơ đồ đơn giản giải thích khái niệm cơ bản về phương thức GET và POST. Bây giờ, để thực hiện các yêu cầu HTTP trong Python, chúng ta có thể sử dụng một số thư viện HTTP như:
Now, to make HTTP requests in python, we can use several HTTP libraries like:
- httplib
- Urllib
- yêu cầu
Thanh lịch và đơn giản nhất của các thư viện được liệt kê ở trên là các yêu cầu. Chúng tôi sẽ sử dụng thư viện yêu cầu trong bài viết này. Để tải xuống và cài đặt thư viện yêu cầu, hãy sử dụng lệnh sau:
pip install requests
Hoặc, tải xuống từ đây và cài đặt thủ công.
Đưa ra yêu cầu nhận
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
2 python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
3python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
4python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
6python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
7python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
9pip install requests0
pip install requests1
pip install requests2
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 pip install requests4
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 pip install requests6
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 pip install requests8
pip install requests9
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 PARAMS = {'address':location}1
PARAMS = {'address':location}2
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 PARAMS = {'address':location}4
PARAMS = {'address':location}5
PARAMS = {'address':location}6
PARAMS = {'address':location}7
PARAMS = {'address':location}6
PARAMS = {'address':location}9
PARAMS = {'address':location}6
//maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university1
PARAMS = {'address':location}6
//maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university3____54
//maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university5
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 PARAMS = {'address':location}4___
r = requests.get[url = URL, params = PARAMS]8
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 PARAMS = {'address':location}4
PARAMS = {'address':location}5
PARAMS = {'address':location}6
PARAMS = {'address':location}7
PARAMS = {'address':location}6
data = r.json[]5
//maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university4
data = r.json[]7
data = r.json[]8
data = r.json[]9
data = {'api_dev_key':API_KEY, 'api_option':'paste', 'api_paste_code':source_code, 'api_paste_format':'python'}0
data = {'api_dev_key':API_KEY, 'api_option':'paste', 'api_paste_code':source_code, 'api_paste_format':'python'}1
data = {'api_dev_key':API_KEY, 'api_option':'paste', 'api_paste_code':source_code, 'api_paste_format':'python'}2
Output:
Ví dụ trên tìm thấy vĩ độ, kinh độ và địa chỉ được định dạng của một địa điểm nhất định bằng cách gửi yêu cầu nhận được API Google Maps. API [Giao diện lập trình ứng dụng] cho phép bạn truy cập các tính năng nội bộ của chương trình theo kiểu hạn chế. Và trong hầu hết các trường hợp, dữ liệu được cung cấp ở định dạng JSON [ký hiệu đối tượng JavaScript] [được triển khai dưới dạng các đối tượng từ điển trong Python!]. Những điểm quan trọng để suy ra:
Important points to infer :
PARAMS = {'address':location}
URL cho một yêu cầu GET thường mang một số tham số với nó. Đối với thư viện yêu cầu, các tham số có thể được định nghĩa là từ điển. Các tham số này sau đó được phân tích cú pháp và thêm vào URL cơ sở hoặc api-endPoint. Để hiểu vai trò tham số, hãy thử in R.URL sau khi đối tượng phản hồi được tạo. Bạn sẽ thấy một cái gì đó như thế này:
To understand the parameters role, try to print r.url after the response object is created. You will see something like this://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
Đây là url thực tế mà yêu cầu nhận được
r = requests.get[url = URL, params = PARAMS]
Ở đây chúng tôi tạo một đối tượng phản hồi ‘R, sẽ lưu trữ phản hồi yêu cầu. Chúng tôi sử dụng phương thức Yêu cầu.get [] vì chúng tôi đang gửi yêu cầu GET. Hai đối số chúng tôi vượt qua là URL và từ điển tham số.
data = r.json[]
Bây giờ, để truy xuất dữ liệu từ đối tượng phản hồi, chúng ta cần chuyển đổi nội dung phản hồi thô thành cấu trúc dữ liệu loại JSON. Điều này đạt được bằng cách sử dụng phương thức JSON []. Cuối cùng, chúng tôi trích xuất các thông tin cần thiết bằng cách phân tích đối tượng loại JSON.
Làm một yêu cầu bài đăng
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
2 python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
3python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
4python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
6data = {'api_dev_key':API_KEY, 'api_option':'paste', 'api_paste_code':source_code, 'api_paste_format':'python'}8
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
7python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
9pip install requests0
pip install requests1
r = requests.post[url = API_ENDPOINT, data = data]5
r = requests.post[url = API_ENDPOINT, data = data]6
r = requests.post[url = API_ENDPOINT, data = data]7
r = requests.post[url = API_ENDPOINT, data = data]8
r = requests.post[url = API_ENDPOINT, data = data]9
r = requests.post[url = API_ENDPOINT, data = data]5
import requests
payload = {'q': 'food'}
r = requests.get['//httpbin.org/get', params=payload]
print [r.text]
01import requests
payload = {'q': 'food'}
r = requests.get['//httpbin.org/get', params=payload]
print [r.text]
02pip install requests2
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 pip install requests4
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 pip install requests6
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 pip install requests8
pip install requests9
python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 PARAMS = {'address':location}1
import requests
payload = {'q': 'food'}
r = requests.get['//httpbin.org/get', params=payload]
print [r.text]
15python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"//httpbin.org/get?q=food"}
5 import requests
payload = {'q': 'food'}
r = requests.get['//httpbin.org/get', params=payload]
print [r.text]
17data = r.json[]7
data = r.json[]8
import requests
payload = {'q': 'food'}
r = requests.get['//httpbin.org/get', params=payload]
print [r.text]
20data = {'api_dev_key':API_KEY, 'api_option':'paste', 'api_paste_code':source_code, 'api_paste_format':'python'}1
import requests
payload = {'q': 'food'}
r = requests.get['//httpbin.org/get', params=payload]
print [r.text]
22Ví dụ này giải thích cách dán Source_Code của bạn lên Pastebin.com bằng cách gửi yêu cầu POST đến API Pastebin. Trước hết, bạn sẽ cần tạo khóa API bằng cách đăng ký tại đây và sau đó truy cập khóa API của bạn tại đây.source_code to pastebin.com by sending POST request to the PASTEBIN API.
First of all, you will need to generate an API key by signing up here and then access your
API key here.
Các tính năng quan trọng của mã này:
data = {'api_dev_key':API_KEY, 'api_option':'paste', 'api_paste_code':source_code, 'api_paste_format':'python'}
Ở đây một lần nữa, chúng ta sẽ cần chuyển một số dữ liệu cho máy chủ API. Chúng tôi lưu trữ dữ liệu này như một từ điển.
r = requests.post[url = API_ENDPOINT, data = data]
Ở đây chúng tôi tạo một đối tượng phản hồi ‘R, sẽ lưu trữ phản hồi yêu cầu. Chúng tôi sử dụng phương thức Yêu cầu.post [] vì chúng tôi đang gửi yêu cầu POST. Hai đối số chúng tôi vượt qua là URL và từ điển dữ liệu.
0import requests payload = {'q': 'food'} r = requests.get['//httpbin.org/get', params=payload] print [r.text]
Đáp lại, máy chủ xử lý dữ liệu được gửi đến và gửi URL Pastebin của Source_Code của bạn có thể được truy cập đơn giản bởi R.Text.source_code which can be simply accessed by r.text .
Yêu cầu. Phương thức có thể được sử dụng cho nhiều tác vụ khác cũng như điền và gửi các biểu mẫu web, đăng trên dòng thời gian FB của bạn bằng API biểu đồ Facebook, v.v.method could be used for many other tasks as well like filling and submitting the web forms, posting on your FB timeline using the Facebook Graph API, etc.
Dưới đây là một số điểm quan trọng cần suy ngẫm về:
- Khi phương thức được nhận, tất cả dữ liệu biểu mẫu được mã hóa vào URL, được thêm vào URL hành động làm tham số chuỗi truy vấn. Với POST, dữ liệu biểu mẫu xuất hiện trong phần thân thông báo của yêu cầu HTTP.action URL as query string parameters. With POST, form data appears within the message body of the HTTP request.
- Trong phương thức GET, dữ liệu tham số được giới hạn trong những gì chúng ta có thể nhét vào dòng yêu cầu [URL]. An toàn nhất để sử dụng ít hơn 2K các tham số, một số máy chủ xử lý tối đa 64K.message body of the HTTP request, not the URL.
- Chỉ các ký tự ASCII được phép gửi dữ liệu trong phương thức GET. Không có hạn chế nào như vậy trong phương thức bài.
- Nhận ít an toàn hơn so với POST vì dữ liệu được gửi là một phần của URL. Vì vậy, không nên sử dụng phương thức khi gửi mật khẩu hoặc thông tin nhạy cảm khác.
Blog này được đóng góp bởi Nikhil Kumar. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên.