Python请求如何在post之前调用js函数来计算一些值?

Python请求如何在post之前调用js函数来计算一些值?,python,python-requests,Python,Python Requests,我使用请求(2.2.1)登录urlhttp://tx3.netease.com/logging.php?action=login,但此url的登录逻辑与Django的csrf令牌机制不同,即: 当您获得此url时,html文本中有两个导入值formhash和sts,这两个值都将用于js函数do_encrypt(在文件http://tx3.netease.com/forumdata/cache/rsa/rsa_min.js)。这很好,我可以通过re轻松抓取它们 html文本的关键部分是: <

我使用请求(2.2.1)登录url
http://tx3.netease.com/logging.php?action=login
,但此url的登录逻辑与Django的csrf令牌机制不同,即:

  • 当您获得此url时,html文本中有两个导入值
    formhash
    sts
    ,这两个值都将用于js函数
    do_encrypt
    (在文件
    http://tx3.netease.com/forumdata/cache/rsa/rsa_min.js
    )。这很好,我可以通过re轻松抓取它们
  • html文本的关键部分是:

    <form method="post" name="login" id="loginform" class="s_clear" onsubmit="do_encrypt('ori_password','password');pwdclear = 1;" action="logging.php?action=login&amp;loginsubmit=yes">
    <input type="hidden" name="formhash" value="91e54489" />
    <input type="hidden" name="referer" value="http://tx3.netease.com/" />
    <input type="hidden" name="sts" id="sts" value="1409414053" />
    <input type="hidden" name="password" id="password" />
    ...
    <input type="password" id="ori_password" name="ori_password" onfocus="clearpwd()" onkeypress="detectCapsLock(event, this)" size="36" class="txt" tabindex="1" autocomplete="off" />
    ...
    </form>
    

    假设您有这样做的权限,请尝试使用
    selenium
    登录,因为我认为这将更符合您最终要做的事情

    from selenium import webdriver
    
    USERNAME = "foo@bar.com"
    PASSWORD = "superelite"
    
    # create a driver
    driver = webdriver.Firefox()
    
    # get the homepage
    driver.get("http://tx3.netease.com/logging.php?action=login")
    
    un_elm = driver.find_element_by_id("username")
    pw_elm = driver.find_element_by_id("ori_password")
    submit = driver.find_element_by_css_selector("[name=loginsubmit]")
    
    un_elm.send_keys(USERNAME)
    pw_elm.send_keys(PASSWORD)
    
    # click submit
    submit.click()
    
    # get the PHPSESSID cookie as that has your login data, if you want to use
    # it elsewhere
    # print driver.get_cookies():
    
    # do something else ...
    

    您要么使用JS引擎执行JS,要么在Python中重新实现相同的逻辑。这两个
    请求
    都不能帮助您。非常感谢。您的方法帮助我成功登录!现在,我将尝试使用
    请求
    的会话:-)
    
    from selenium import webdriver
    
    USERNAME = "foo@bar.com"
    PASSWORD = "superelite"
    
    # create a driver
    driver = webdriver.Firefox()
    
    # get the homepage
    driver.get("http://tx3.netease.com/logging.php?action=login")
    
    un_elm = driver.find_element_by_id("username")
    pw_elm = driver.find_element_by_id("ori_password")
    submit = driver.find_element_by_css_selector("[name=loginsubmit]")
    
    un_elm.send_keys(USERNAME)
    pw_elm.send_keys(PASSWORD)
    
    # click submit
    submit.click()
    
    # get the PHPSESSID cookie as that has your login data, if you want to use
    # it elsewhere
    # print driver.get_cookies():
    
    # do something else ...