【发布时间】:2014-10-23 22:38:00
【问题描述】:
我有一个文件 gather.htm,它是一个有效的 HTML 文件,包含 header/body 和 forms。如果我双击桌面上的文件,它会在网络浏览器中正确打开,自动提交form 数据(通过<SCRIPT LANGUAGE="Javascript">document.forms[2].submit();</SCRIPT>),页面会刷新请求的数据。
我希望能够让 Python 使用 gather.htm 进行 requests.post(url) 调用。但是,我的研究和反复试验并没有提供任何解决方案。
这是如何实现的?
我已经尝试过这些方法(基于网络上的示例)。我怀疑我在这里遗漏了一些简单的东西!
myUrl = 'www.somewhere.com'
filename='/Users/John/Desktop/gather.htm'
f = open (filename)
r = requests.post(url=myUrl, data = {'title':'test_file'}, files = {'file':f})
print r.status_code
print r.text
还有:
htmfile = 'file:///Users/John/Desktop/gather.htm'
files = {'file':open('gather.htm')}
webbrowser.open(url,new=2)
response = requests.post(url)
print response.text
请注意,在上面的第二个示例中,webbrowser.open() 调用正常工作,但 requests.post 不能正常工作。
看来我尝试的一切都以同样的方式失败了——打开了 URL,页面返回了默认数据。该网站似乎从未收到gather.htm 文件。
【问题讨论】:
-
你能给我们看一下描述表单的 HTML 吗?
-
第一个代码块返回的状态码是什么?
-
我将其缩短并删除了 URL。还有一些其他的表格没有显示,底部的表格[2].submit()。
-
document.forms[2].submit();