多个POST请求,第二个请求获得404错误代码

时间:2018-06-19 05:02:44

标签: python-3.x request python-requests http-status-code-404

我是python的新手,我遇到了同样的问题,这个问题在GitHub请求中被提出来了。我正在尝试对初次登录后重定向到安全问题的网站进行身份验证。初始登录页面和后续页面都使用相同的"操作URL"在第二篇帖子请求我收到404,这是我的代码,任何帮助将不胜感激,在询问GitHub上的问题后,他们说这是一个问题,因为它不是在他们的结束。 (虽然他们的GitHub上有一个关于此的问题):

from bs4 import BeautifulSoup as bs
import requests
import time
sources = ["https://www.dandh.com/v4/view?pageReq=dhMainNS"]
req = requests.Session()


def login():
    authentication_url = "https://www.dandh.com/v4/dh"

    header = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; rv:60.0)      Gecko/20100101 Firefox/60.0"}

    payload = {"Login": "12345",
           "PW": "12345",
           "Request": "Login"}

    payload2 = {"securityAnswer": "12345",
            "Request": "postForm"}

    req.post(authentication_url, data=payload, headers=header)

    time.sleep(3)

    req.post(authentication_url, data=payload2, headers=header)

    time.sleep(3)


def print_object(sources):
    for url in sources:
        soup_object = bs(req.get(url).text, "html.parser")
        print(soup_object.get_text())


def main():
    login()
    print_object(sources)

main()

1 个答案:

答案 0 :(得分:0)

第1部分

浏览网站后,发现问题的严重部分取决于payload2,您只需向其中添加另一个项目:"formName":"loginChallengeValidation",因此总体payload2应该看起来像这样的东西:

payload2 = {"formName":"loginChallengeValidation","securityAnswer": your_security_answer,
            "Request": "postForm"}

这将阻止您获取状态代码404。希望这会有所帮助。


第2部分

尽管这是您所提问题中的问题,但我仍然怀疑这是您真正想要的(因为第1部分中的代码会将您重定向到另一种验证表单)。为了访问网站本身,您必须添加以下几行:

header2 = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; rv:60.0)      Gecko/20100101 Firefox/60.0", "Referer":"https://www.dandh.com/v4/view?pageReq=LoginChallengeValidation"}

req.post("https://www.dandh.com/v4/view?pageReq=LoginChallengeValidation", headers=header)

因此您的最终代码应如下所示:

from bs4 import BeautifulSoup as bs
import requests
import time
sources = ["https://www.dandh.com/v4/view?pageReq=dhMainNS"]
req = requests.Session()


def login():
    authentication_url = "https://www.dandh.com/v4/dh"

    header = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; rv:60.0)      Gecko/20100101 Firefox/60.0"}
    header2 = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; rv:60.0)      Gecko/20100101 Firefox/60.0", "Referer":"https://www.dandh.com/v4/view?pageReq=LoginChallengeValidation"}

    payload = {"Login": your_username,
           "PW": your_pasword,
           "Request": "Login"}

    payload2 = {"formName":"loginChallengeValidation","securityAnswer": your_security_answer,
            "Request": "postForm", "btContinue": ""}

    req.post(authentication_url, data=payload, headers=header)
    req.post("https://www.dandh.com/v4/view?pageReq=LoginChallengeValidation", headers=header)

    time.sleep(3)

    req.post(authentication_url, data=payload2, headers=header2)

    time.sleep(3)


def print_object(sources):
    for url in sources:
        soup_object = bs(req.get(url).text, "html.parser")
        print(soup_object.get_text())


def main():
    login()
    print_object(sources)

main()

(PS:您应将your_usernameyour_passwordyour_security_answer替换为您的凭据) 另外,我想指出,我认为time.sleep(3)在代码中没有用。

真的希望这会有所帮助。