Hướng dẫn how does python requests post work? - Làm thế nào để đăng yêu cầu python hoạt động?

Mô -đun yêu cầu


Thí dụ

Thực hiện yêu cầu bài đăng đến một trang web và trả về văn bản phản hồi:

Nhập yêu cầu

url = 'https://www.w3schools.com/python/demopage.php' myObj = {'someKey': 'somervalue'}
myobj = {'somekey': 'somevalue'}

x = requests.post (url, json = myobj)

in (x.text)

Chạy ví dụ »


Định nghĩa và cách sử dụng

Phương thức post() gửi yêu cầu POST đến URL được chỉ định.

Phương thức post() được sử dụng khi bạn muốn gửi một số dữ liệu đến máy chủ.


Cú pháp

requests.post (url, data = {key: value}, json = {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.


Giá trị tham số

Tham sốSự mô tả
URLThử nóYêu cầu. URL của yêu cầu
dữ liệuThử nóYêu cầu. URL của yêu cầu
dữ liệuThử nóYêu cầu. URL của yêu cầu
dữ liệuThử nóYêu cầu. URL của yêu cầu
dữ liệuThử nóYêu cầu. URL của yêu cầu
Default
PARAMS = {'address':location}
0 (allowing redirects)
dữ liệuThử nóYêu cầu. URL của yêu cầu
Default
PARAMS = {'address':location}
1
dữ liệuThử nóYêu cầu. URL của yêu cầu
Default
PARAMS = {'address':location}
1
dữ liệuThử nóYêu cầu. URL của yêu cầu
Default
PARAMS = {'address':location}
1
dữ liệuThử nóYêu cầu. URL của yêu cầu
Default
PARAMS = {'address':location}
1
dữ liệuThử nóYêu cầu. URL của yêu cầu
Default
PARAMS = {'address':location}
1
dữ liệuThử nóYêu cầu. URL của yêu cầu
Default
PARAMS = {'address':location}
6
dữ liệuThử nóYêu cầu. URL của yêu cầu
Default
PARAMS = {'address':location}
1 which means the request will continue until the connection is closed
dữ liệuKhông bắt buộc. Một từ điển, danh sách các bộ dữ liệu, byte hoặc một đối tượng tệp để gửi đến URL được chỉ định
Try it
json
Default
PARAMS = {'address':location}
0

Không bắt buộc. Một đối tượng JSON để gửi đến URL được chỉ định

các tập tin


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:

  1. Nhận: Để yêu cầu dữ liệu từ máy chủ. : to request data from the server.
  2. Đă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ư:

Hướng dẫn how does python requests post work? - Làm thế nào để đăng yêu cầu python hoạt động?

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

PARAMS = {'address':location}
9
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
0

http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
1
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
3

http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
4
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
6
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
7
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
8

http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
9
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
r = requests.get(url = URL, params = PARAMS)
1
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
r = requests.get(url = URL, params = PARAMS)
3
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
r = requests.get(url = URL, params = PARAMS)
5

r = requests.get(url = URL, params = PARAMS)
6
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
r = requests.get(url = URL, params = PARAMS)
8

r = requests.get(url = URL, params = PARAMS)
9
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
data = r.json()
1
data = r.json()
2
data = r.json()
3
data = r.json()
4
data = r.json()
3
data = r.json()
6
data = r.json()
3
data = r.json()
8
data = r.json()
3
data = {'api_dev_key':API_KEY,
        'api_option':'paste',
        'api_paste_code':source_code,
        'api_paste_format':'python'}
05051

data = {'api_dev_key':API_KEY,
        'api_option':'paste',
        'api_paste_code':source_code,
        'api_paste_format':'python'}
2
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
data = r.json()
1
data = r.json()
2
data = r.json()
3
data = r.json()
4
data = r.json()
3
data = r.json()
6
data = r.json()
3
data = r.json()
8__

r = requests.post(url = API_ENDPOINT, data = data)
5
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
data = r.json()
1
data = r.json()
2
data = r.json()
3
data = r.json()
4
data = r.json()
3
pastebin_url = r.text
2
data = {'api_dev_key':API_KEY,
        'api_option':'paste',
        'api_paste_code':source_code,
        'api_paste_format':'python'}
1

pastebin_url = r.text
4
pastebin_url = r.text
5
pastebin_url = r.text
6

pastebin_url = r.text
7
pastebin_url = r.text
8
pastebin_url = r.text
9

Hướng dẫn how does python requests post work? - Làm thế nào để đăng yêu cầu python hoạt động?

Output:

Hướng dẫn how does python requests post work? - Làm thế nào để đăng yêu cầu python hoạt động?

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:

    http://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

PARAMS = {'address':location}
9
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
0

post()2

http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2 post()4

post()5

http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2

r = requests.get(url = URL, params = PARAMS)
6
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
6post()0post()1

post()2post()3post()4post()5post()6

post()2post()8post()9

http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
9
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
PARAMS = {'address':location}
07
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
PARAMS = {'address':location}
09
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
PARAMS = {'address':location}
11

PARAMS = {'address':location}
12
http://maps.googleapis.com/maps/api/geocode/json?address=delhi+technological+university
2
PARAMS = {'address':location}
14

pastebin_url = r.text
4
pastebin_url = r.text
5
PARAMS = {'address':location}
17
pastebin_url = r.text
8
PARAMS = {'address':location}
19

Ví 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.

  • pastebin_url = 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.


Làm thế nào để bạn thực hiện một yêu cầu bài viết trong Python?

Để gửi yêu cầu POST bằng thư viện Python Requests, bạn nên gọi phương thức Yêu cầu.post () và chuyển URL đích làm tham số đầu tiên và dữ liệu bài đăng với tham số Data =.call the requests. post() method and pass the target URL as the first parameter and the POST data with the data= parameter.

Làm thế nào để yêu cầu có được công việc trong Python?

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ủ.Hãy trình bày cách thực hiện yêu cầu nhận đến điểm cuối.Phương thức GET được sử dụng để truy xuất thông tin từ máy chủ đã cho bằng URI đã cho.Phương thức GET gửi thông tin người dùng được mã hóa được thêm vào yêu cầu trang.GET method is used to retrieve information from the given server using a given URI. The GET method sends the encoded user information appended to the page request.

Bài đăng và nhận được trong Python là gì?

Nhận: Để yêu cầu dữ liệu từ máy chủ.post: Để gửi dữ liệu để được xử lý đến máy chủ. POST : to submit data to be processed to the server.

Những lợi thế của phương thức bài trong Python là gì?

Ưu điểm của việc sử dụng phương thức POST Nó an toàn hơn là nhận vì thông tin nhập vào người dùng không bao giờ hiển thị trong chuỗi truy vấn URL hoặc trong nhật ký máy chủ.Có giới hạn lớn hơn nhiều về lượng dữ liệu có thể được truyền và người ta có thể gửi dữ liệu văn bản cũng như dữ liệu nhị phân (tải lên tệp) bằng POST.It is more secure than GET because user-entered information is never visible in the URL query string or in the server logs. There is a much larger limit on the amount of data that can be passed and one can send text data as well as binary data (uploading a file) using POST.