我使用的是 Python 3.3 和 Requests 2.2.1。
我正在尝试发布到以 .jsp 结尾的网站,然后更改为 .doh 结尾。使用相同的基本请求代码大纲,我能够成功登录并抓取其他网站,但该网站上的 javascript 部分无法正常工作。这是我的代码:
import requests
url = 'https://prodpci.etimspayments.com/pbw/include/sanfrancisco/input.jsp'
payload = {'plateNumber':'notshown', 'statePlate':'CA'} #tried CA and California
s = requests.Session() #Tried 'session' and 'Session' following different advice
post = s.post(url, data=payload)
r = s.get('https://prodpci.etimspayments.com/pbw/include/sanfrancisco/input.jsp')
print(r.text)
最后,通过firefox浏览器手动向网页输入数据时,页面发生变化,url变为https://prodpci.etimspayments.com/pbw/inputAction.doh ,如果您在输入车牌后被重定向到那里,则只有内容。
从打印的文本中,我知道我正在从页面获取内容,就像没有发布任何内容一样,但是在我发布有效负载后我需要页面的内容。 对于 POST 负载,我是否需要包含类似 'submit':'submit' 的内容来模拟点击搜索按钮?
考虑到我 POST 到的网址,我是否从正确的网址执行 GET 请求?
最佳答案
您正在发出 POST 请求,然后发出另一个 GET 请求,这就是您获得与表单相同的页面的原因。
response = s.post(url, data=payload)
print(response.text)
此外,如果您检查表单标记,您会发现它的操作是 /pbw/inputAction.doh
,此外,表单还会从隐藏
输入中发送一些参数。因此,您应该在请求中使用该 URL,并可能使用来自 hidden
输入的值。
使用下一个代码,我能够检索与通过浏览器中的常规请求相同的响应:
import requests
url = 'https://prodpci.etimspayments.com/pbw/inputAction.doh'
payload = {
'plateNumber': 'notshown',
'statePlate': 'CA',
'requestType': 'submit',
'clientcode': 19,
'requestCount': 1,
'clientAccount': 5,
}
s = requests.Session()
response = s.post(url, data=payload)
print(response.text)
在通过表单发出相同请求后,您可以在浏览器中看到相同的内容:
...
<td colspan="2"> <li class="error">Plate is not found</li></td>
...
关于javascript - 使用 Python 请求库发布到 .jsp 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24772500/