Hướng dẫn how to send a post request to a url in python - làm thế nào để gửi một yêu cầu bài đăng đến một url trong python

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 = '//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

>>> import requests
>>> r = requests.post["//bugs.python.org", data={'number': '12524', 'type': 'issue', 'action': 'show'}]
>>> print[r.status_code, r.reason]
200 OK
>>> print[r.text[:300] + '...']





Issue 12524: change httplib docs POST example - Python tracker


>> 
1 đượ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
>>> import requests
>>> r = requests.post["//bugs.python.org", data={'number': '12524', 'type': 'issue', 'action': 'show'}]
>>> print[r.status_code, r.reason]
200 OK
>>> print[r.text[:300] + '...']





Issue 12524: change httplib docs POST example - Python tracker


>> 
4
dữ liệuThử nóYêu cầu. URL của yêu cầu
Default
>>> import requests
>>> r = requests.post["//bugs.python.org", data={'number': '12524', 'type': 'issue', 'action': 'show'}]
>>> print[r.status_code, r.reason]
200 OK
>>> print[r.text[:300] + '...']





Issue 12524: change httplib docs POST example - Python tracker


>> 
4
dữ liệuThử nóYêu cầu. URL của yêu cầu
Default
>>> import requests
>>> r = requests.post["//bugs.python.org", data={'number': '12524', 'type': 'issue', 'action': 'show'}]
>>> print[r.status_code, r.reason]
200 OK
>>> print[r.text[:300] + '...']





Issue 12524: change httplib docs POST example - Python tracker


>> 
4
dữ liệuThử nóYêu cầu. URL của yêu cầu
Default
>>> import requests
>>> r = requests.post["//bugs.python.org", data={'number': '12524', 'type': 'issue', 'action': 'show'}]
>>> print[r.status_code, r.reason]
200 OK
>>> print[r.text[:300] + '...']





Issue 12524: change httplib docs POST example - Python tracker


>> 
4 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
>>> import requests
>>> r = requests.post["//bugs.python.org", data={'number': '12524', 'type': 'issue', 'action': 'show'}]
>>> print[r.status_code, r.reason]
200 OK
>>> print[r.text[:300] + '...']





Issue 12524: change httplib docs POST example - Python tracker


>> 

Đã trả lời ngày 4 tháng 7 năm 2012 lúc 8:08Jul 4, 2012 at 8:08

7

Đây là một giải pháp không có bất kỳ phụ thuộc PIP bên ngoài nào, nhưng chỉ hoạt động trong Python 3+ [Python 2 sẽ không hoạt động]:

from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = '//httpbin.org/post' # Set destination URL here
post_fields = {'foo': 'bar'}     # Set POST fields here

request = Request[url, urlencode[post_fields].encode[]]
json = urlopen[request].read[].decode[]
print[json]

Đầu ra mẫu:

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "foo": "bar"
  }, 
  "headers": {
    "Accept-Encoding": "identity", 
    "Content-Length": "7", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Python-urllib/3.3"
  }, 
  "json": null, 
  "origin": "127.0.0.1", 
  "url": "//httpbin.org/post"
}

Đã trả lời ngày 17 tháng 4 năm 2016 lúc 15:30Apr 17, 2016 at 15:30

Stilstilstil

5.2263 huy hiệu vàng36 Huy hiệu bạc43 Huy hiệu đồng3 gold badges36 silver badges43 bronze badges

0

Bạn không thể đạt được các yêu cầu POST bằng cách sử dụng

from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = '//httpbin.org/post' # Set destination URL here
post_fields = {'foo': 'bar'}     # Set POST fields here

request = Request[url, urlencode[post_fields].encode[]]
json = urlopen[request].read[].decode[]
print[json]
2 [chỉ để nhận], thay vào đó hãy thử sử dụng mô -đun
from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = '//httpbin.org/post' # Set destination URL here
post_fields = {'foo': 'bar'}     # Set POST fields here

request = Request[url, urlencode[post_fields].encode[]]
json = urlopen[request].read[].decode[]
print[json]
3, ví dụ:

Ví dụ 1.0:

import requests

base_url="www.server.com"
final_url="/{0}/friendly/{1}/url".format[base_url,any_value_here]

payload = {'number': 2, 'value': 1}
response = requests.post[final_url, data=payload]

print[response.text] #TEXT/HTML
print[response.status_code, response.reason] #HTTP

Ví dụ 1.2:

>>> import requests

>>> payload = {'key1': 'value1', 'key2': 'value2'}

>>> r = requests.post["//httpbin.org/post", data=payload]
>>> print[r.text]
{
  ...
  "form": {
    "key2": "value2",
    "key1": "value1"
  },
  ...
}

Ví dụ 1.3:

>>> import json

>>> url = '//api.github.com/some/endpoint'
>>> payload = {'some': 'data'}

>>> r = requests.post[url, data=json.dumps[payload]]

không thấm nước

4.7545 Huy hiệu vàng30 Huy hiệu bạc28 Huy hiệu Đồng5 gold badges30 silver badges28 bronze badges

Đã trả lời ngày 10 tháng 4 năm 2016 lúc 7:49Apr 10, 2016 at 7:49

d1jhoni1bd1jhoni1bd1jhoni1b

7,1211 Huy hiệu vàng51 Huy hiệu bạc32 Huy hiệu đồng1 gold badge51 silver badges32 bronze badges

1

Sử dụng thư viện

from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = '//httpbin.org/post' # Set destination URL here
post_fields = {'foo': 'bar'}     # Set POST fields here

request = Request[url, urlencode[post_fields].encode[]]
json = urlopen[request].read[].decode[]
print[json]
3 để nhận, đăng, đặt hoặc xóa bằng cách nhấn điểm cuối API REST. Vượt qua URL điểm cuối API còn lại trong
from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = '//httpbin.org/post' # Set destination URL here
post_fields = {'foo': 'bar'}     # Set POST fields here

request = Request[url, urlencode[post_fields].encode[]]
json = urlopen[request].read[].decode[]
print[json]
5, tải trọng [DIRT] trong
from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = '//httpbin.org/post' # Set destination URL here
post_fields = {'foo': 'bar'}     # Set POST fields here

request = Request[url, urlencode[post_fields].encode[]]
json = urlopen[request].read[].decode[]
print[json]
6 và tiêu đề/siêu dữ liệu trong
from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = '//httpbin.org/post' # Set destination URL here
post_fields = {'foo': 'bar'}     # Set POST fields here

request = Request[url, urlencode[post_fields].encode[]]
json = urlopen[request].read[].decode[]
print[json]
7

import requests, json

url = "bugs.python.org"

payload = {"number": 12524, 
           "type": "issue", 
           "action": "show"}

header = {"Content-type": "application/x-www-form-urlencoded",
          "Accept": "text/plain"} 

response_decoded_json = requests.post[url, data=payload, headers=header]
response_json = response_decoded_json.json[]
 
print[response_json]

Pikamander2

6.5113 Huy hiệu vàng43 Huy hiệu bạc65 Huy hiệu Đồng3 gold badges43 silver badges65 bronze badges

Đã trả lời ngày 5 tháng 12 năm 2018 lúc 8:38Dec 5, 2018 at 8:38

PranzellpranzellPranzell

2.08815 huy hiệu bạc21 Huy hiệu đồng15 silver badges21 bronze badges

3

Tên conteines từ điển dữ liệu của bạn tên của các trường đầu vào biểu mẫu, bạn chỉ cần tiếp tục đúng giá trị của chúng để tìm kết quả. Chế độ xem tiêu đề Xem cấu hình trình duyệt để truy xuất loại dữ liệu bạn khai báo. Với thư viện yêu cầu, thật dễ dàng để gửi bài đăng:

import requests

url = "//bugs.python.org"
data = {'@number': 12524, '@type': 'issue', '@action': 'show'}
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept":"text/plain"}
response = requests.post[url, data=data, headers=headers]

print[response.text]

Thông tin thêm về đối tượng yêu cầu: //requests.readthedocs.io/en/master/api/

Đã trả lời ngày 20 tháng 1 năm 2020 lúc 14:28Jan 20, 2020 at 14:28

Nếu bạn không muốn sử dụng mô -đun, bạn phải cài đặt như

from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = '//httpbin.org/post' # Set destination URL here
post_fields = {'foo': 'bar'}     # Set POST fields here

request = Request[url, urlencode[post_fields].encode[]]
json = urlopen[request].read[].decode[]
print[json]
3 và trường hợp sử dụng của bạn rất cơ bản, thì bạn có thể sử dụng
from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = '//httpbin.org/post' # Set destination URL here
post_fields = {'foo': 'bar'}     # Set POST fields here

request = Request[url, urlencode[post_fields].encode[]]
json = urlopen[request].read[].decode[]
print[json]
9

urllib2.urlopen[url, body]

Xem tài liệu cho

from urllib.parse import urlencode
from urllib.request import Request, urlopen

url = '//httpbin.org/post' # Set destination URL here
post_fields = {'foo': 'bar'}     # Set POST fields here

request = Request[url, urlencode[post_fields].encode[]]
json = urlopen[request].read[].decode[]
print[json]
9 tại đây: //docs.python.org/2/l Library/urllib2.html.

Đã trả lời ngày 17 tháng 1 năm 2019 lúc 20:17Jan 17, 2019 at 20:17

PhilPhilPhil

2.0692 Huy hiệu vàng22 Huy hiệu bạc34 Huy hiệu đồng2 gold badges22 silver badges34 bronze badges

Bạn có thể sử dụng thư viện yêu cầu để thực hiện yêu cầu POST. Nếu bạn có chuỗi JSON trong tải trọng, bạn có thể sử dụng json.dumps [tải trọng], đây là dạng tải trọng dự kiến.

>>> import requests
>>> r = requests.post["//bugs.python.org", data={'number': '12524', 'type': 'issue', 'action': 'show'}]
>>> print[r.status_code, r.reason]
200 OK
>>> print[r.text[:300] + '...']





Issue 12524: change httplib docs POST example - Python tracker


Bài Viết Liên Quan

Chủ Đề