为了模拟在不填写任何表单的情况下单击“搜索”按钮,这将向页面添加数据。我在chrome开发工具中查看network选项卡时点击按钮,得到了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'))
这个解决方案输出页面的HTML,但不输出我想从POST请求中检索的任何数据。