Python请求-org.apache.struts.taglib.html.TOKEN问题

时间:2018-08-07 10:59:44

标签: python-3.x python-requests

第一次在这里发布,因此如果我礼节有误,请提前致歉。

我正在使用请求在python 3中编写一些代码,以登录到网站,并且登录后应该返回另一个页面。

我已经使用Google Chrome浏览器开发人员工具来查看有效载荷中需要包含哪些表单数据,而我认为引起问题的是org.apache.struts.taglib.html.TOKEN,这在每个表单提交。

有人知道如何解决这个问题吗?还是另一个问题? 目前,它返回的页面告诉我“ Details Incorrect”。我已经使用这些详细信息手动登录到站点,以记录登录期间发送的数据。

我的代码如下。

import requests

with requests.Session() as s:

payload = {"org.apache.struts.taglib.html.TOKEN": this is unique on each form submission,
           "loginRegNo": xxxxxxx, "loginPin": xxxxxx}
headers = {"Accept": "text/html",
           "Accept-Encoding": "gzip, deflate, br",
           "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
           "Cache-Control": "no-cache",
           "Connection": "keep-alive",
           "Content-Length": "105",
           "2Content-Type": "application/x-www-form-urlencoded",
           "Cookie": "JSESSIONID=xxxxxx,
           "Host": "www.website.ie",
           "Origin": "https://www.website.ie",
           "Pragma": "no-cache",
           "Referer": "https://www.website.ie/OMT/omt.do",
           "Upgrade-Insecure-Requests": "1",
           "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                         "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36}"
           }

p = s.post("https://www.website.ie/OMT/omt.do", data=payload, headers=headers, cookies=s.cookies)
# print the status code to see if it's successful
print(p.status_code)

r = s.get("https://www.website.ie/OMT/login.do", cookies=s.cookies)
print(r.text)
print(r.url)

print(r.status_code)

2 个答案:

答案 0 :(得分:1)

我认为你应该

 - p = s.get("https://www.website.ie/OMT/omt.do")
 - extract the token generated for that session from the org.apache.struts.taglib.html.TOKEN input element of p
 - add the extracted token to the payload, beside loginRegNo and loginPin
 - (you might not need to add Cookie and Content-Length headers)
 - s.post("https://www.website.ie/OMT/login.do", data=payload, headers=headers, cookies=s.cookies)

请注意,我获得https://www.website.ie/OMT/omt.do并针对https://www.website.ie/OMT/login.do进行开机自检。

祝你好运!

答案 1 :(得分:0)

您可能可以通过beautifulsoup获得令牌。

使用urllib2获取页面,通过beautifulsoup对其进行解析,并在html文档中找到令牌

它可能看起来像这样:

soup.body.input

您将选择输出,并希望存储令牌的位置。在html文档中找到令牌的位置后,您将可以通过以下方式访问它 DB::table("countries")->whereIn('id',explode(",",$ids))->get()->delete(); 或类似的东西。

希望有帮助:)