【问题标题】:Python requests - org.apache.struts.taglib.html.TOKEN issuePython 请求 - org.apache.struts.taglib.html.TOKEN 问题
【发布时间】:2018-08-07 10:59:43
【问题描述】:

第一次在这里发帖,如果我有任何礼仪错误,请提前道歉。

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

我使用 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”里放了什么?

标签: python-3.x python-requests


【解决方案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 并针对 https://www.website.ie/OMT/login.do 发布。

祝你好运!

【讨论】:

    【解决方案2】:

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

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

    它可能看起来像这样:

    import urllib2
    from bs4 import BeautifulSoup
    
    f = urllib2.urlopen('..') # your url
    
    soup = BeautifulSoup(f, 'html.parser')
    
    print(soup.prettify())
    

    您将看到输出并希望令牌存储在哪里。在 html 文档中找到令牌的位置后,您将能够通过以下方式访问它 soup.body.input 或类似的东西。

    希望有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      相关资源
      最近更新 更多