Python - 使用 Python 3 urllib 发出 POST 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36484184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - make a POST request using Python 3 urllib
提问by Daniel Paczuski Bak
I am trying to make a POST request to the following page:http://search.cpsa.ca/PhysicianSearch
我正在尝试向以下页面发出 POST 请求:http: //search.cpsa.ca/PhysicianSearch
In order to simulate clicking the 'Search' button without filling out any of the form, which adds data to the page. I got the POST header information by clicking on the button while looking at the network tab in chrome developer tools. The reason I'm posting this instead of just copying solutions from the other similar problems is that I believe I may have not gotten the correct header information. Is it properly formatted and did I grab the right information? I've never made a POST request before.
为了模拟单击“搜索”按钮而不填写任何表单,从而向页面添加数据。我通过在 chrome 开发人员工具中查看网络选项卡时单击按钮来获取 POST 标头信息。我发布这个而不是仅仅从其他类似问题中复制解决方案的原因是我相信我可能没有得到正确的标题信息。它的格式是否正确,我是否获取了正确的信息?我以前从未发出过 POST 请求。
This is what I've managed to piece together:
这是我设法拼凑起来的:
import urllib.parse
import urllib.request
data = urllib.parse.urlencode({'Host': 'search.cpsa.ca', 'Connection': 'keep-alive', 'Content-Length': 23796,
'Origin': 'http://search.cpsa.ca', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cahce-Control': 'no-cache', 'X-Requested-With': 'XMLHttpRequest',
'X-MicrosoftAjax': 'Delta=true', 'Accept': '*/*',
'Referer': 'http://search.cpsa.ca/PhysicianSearch',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-GB,en-US;q=0.8,en;q=0.6',
'Cookie': 'ASP.NET_SessionId=kcwsgio3dchqjmyjtwue402c; _ga=GA1.2.412607756.1459536682; _gat=1'})
url = "http://www.musi-cal.com/cgi-bin/query?%s"
data = data.encode('ascii')
with urllib.request.urlopen("http://search.cpsa.ca/PhysicianSearch", data) as f:
print(f.read().decode('utf-8'))
This solution outputs the page's HTML, but not with any of the data I wanted to retrieve from the POST request.
此解决方案输出页面的 HTML,但不输出我想从 POST 请求中检索的任何数据。
回答by C Panda
This is how you do it.
这就是你如何做到的。
from urllib import request, parse
data = parse.urlencode(<your data dict>).encode()
req = request.Request(<your url>, data=data) # this will make the method "POST"
resp = request.urlopen(req)
回答by Centos Newbie
Thank you C Panda. You really made it easy for me to learn this module.
谢谢C熊猫。你真的让我很容易学习这个模块。
I released the dictionary that we pass does not encode for me. I had to do a minor change -
我发布了我们传递的字典,它没有为我编码。我不得不做一个小的改变 -
from urllib import request, parse
import json
# Data dict
data = { 'test1': 10, 'test2': 20 }
# Dict to Json
# Difference is { "test":10, "test2":20 }
data = json.dumps(data)
# Convert to String
data = str(data)
# Convert string to byte
data = data.encode('utf-8')
# Post Method is invoked if data != None
req = request.Request(<your url>, data=data)
# Response
resp = request.urlopen(req)
回答by iphaaw
The above code encoded the JSON string with some extra \" that caused me a lot of problems. This looks like a better way of doing it:
上面的代码用一些额外的 \" 编码了 JSON 字符串,这给我带来了很多问题。这看起来是一个更好的方法:
from urllib import request, parse
url = "http://www.example.com/page"
data = {'test1': 10, 'test2': 20}
data = parse.urlencode(data).encode()
req = request.Request(url, data=data)
response = request.urlopen(req)
print (response.read())
回答by shifu.zheng
It failed when I use urlencode. So I use the following code to make a POST call in Python3:
当我使用 urlencode 时它失败了。所以我使用以下代码在 Python3 中进行 POST 调用:
from urllib import request, parse
data = b'{"parameter1": "test1", "parameter2": "test2"}'
req = request.Request("http://www.musi-cal.com/cgi-bin/query?%s", data)
resp = request.urlopen(req).read().decode('utf-8')
print(resp)