Python - 使用Python 3 urllib发出POST请求

Dan*_*Bak 15 python post google-chrome http web-scraping

我正在尝试向以下页面发出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': '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'))
Run Code Online (Sandbox Code Playgroud)

此解决方案输出页面的HTML,但不包含我想要从POST请求中检索的任何数据.

小智 53

这就是你如何做到的.

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)
Run Code Online (Sandbox Code Playgroud)

  • 如果我想发出一个正文为空的 POST 请求怎么办? (4认同)
  • request.Request(..., method='POST'). https://docs.python.org/3/library/urllib.request.html#urllib.request.Request.method (3认同)

小智 10

谢谢C熊猫。您确实使我学习此模块变得容易。

我发布了我们通过的字典不为我编码。我不得不做些小改动-

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)
Run Code Online (Sandbox Code Playgroud)

  • 应该真的是一个评论 (5认同)
  • 你可以在一个命令中结合你对 `data` 所做的 3 种操作: `request.urlopen(url='your url', data=bytes(json.dumps(dict_obj), encoding='utf-8') )` (4认同)
  • 如果服务在它接受的内容类型方面很严格并且它​​是 json,那么还需要以下内容:`req.add_header('Content-Type', 'application/json')` https://stackoverflow.com/a/ 9746432/158328 (2认同)

iph*_*aaw 9

上面的代码用一些额外的 \" 编码了 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())
Run Code Online (Sandbox Code Playgroud)


Jos*_*ush 5

设置method="POST"request.Request().


发送没有正文的 POST 请求:

from urllib import request

req = request.Request('https://postman-echo.com/post', method="POST")
r = request.urlopen(req)
content = r.read()
print(content)
Run Code Online (Sandbox Code Playgroud)

使用 json 正文发送 POST 请求:

from urllib import request

req = request.Request('https://postman-echo.com/post', method="POST")
req.add_header('Content-Type', 'application/json')
data = {
    "hello": "world"
}
data = json.dumps(data)
data = data.encode()
r = request.urlopen(req, data=data)
content = r.read()
print(content)
Run Code Online (Sandbox Code Playgroud)