Python requests - org.apache.struts.taglib.html.TOKEN问题

5

第一次在这里发帖,如有任何礼仪方面的错误,请事先谅解。

我正在使用Python 3和Requests库编写代码,在网站上登录后应该会返回给我另一个页面。

我使用Google Chrome开发者工具查看了必须包含在有效负载中的表单数据,我认为问题的原因是org.apache.struts.taglib.html.TOKEN,它在每个表单提交时都是唯一的。

有人知道如何解决这个问题吗?还是另一个问题?当前它让我返回一个页面,告诉我“详细信息不正确”。但我已经手动使用这些详细信息登录了该网站,以记录登录期间发送的数据。

我的代码如下。

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)

你要放什么到 "org.apache.struts.taglib.html.TOKEN" 中? - ax.
2个回答

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)

请注意,我使用GET方法访问https://www.website.ie/OMT/omt.do并使用POST方法访问https://www.website.ie/OMT/login.do
祝你好运!

0

你可以通过beautifulsoup获取token。

使用urllib2获取页面,通过beautifulsoup解析并在HTML文档中找到token。

它可能看起来像这样:

import urllib2
from bs4 import BeautifulSoup

f = urllib2.urlopen('..') # your url

soup = BeautifulSoup(f, 'html.parser')

print(soup.prettify())

你将会看到输出并且希望能够找到token存储的位置。当你在html文档中找到token的位置后,你就可以通过soup.body.input或类似的方式来访问它。
希望这有所帮助 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接