Python - 使用Python 3 urllib发出POST请求 [英] Python - make a POST request using Python 3 urllib
问题描述
我正在尝试向以下页面发送POST请求: http://search.cpsa.ca/PhysicianSearch
为了模拟点击'搜索'按钮而不填写任何表单,它将数据添加到页面中。通过在Chrome开发人员工具中查看网络选项卡时单击该按钮,我可以获得POST标题信息。我发布这个的原因,而不是从其他类似问题复制解决方案是我相信我可能没有得到正确的标题信息。格式正确吗?我是否抓取了正确的信息?我以前从来没有发过POST请求。
这是我拼凑起来的东西:
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':'应用程序/ 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-编码':'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'))
这个解决方案输出页面的HTML,但不是我想从POST请求中检索任何数据。
这就是您的操作方式。
from urllib import request,parse
pre>
data = parse.urlencode(< your data dict>)。encode()
req = request.Request(< your url> data = data)#这将使方法POST
resp = request.urlopen(req)
I am trying to make a POST request to the following page: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.
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.
解决方案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)
这篇关于Python - 使用Python 3 urllib发出POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!