我想在http://www.ppdai.com/Login.aspx登录网站 我已经尝试过一些解决方案,比如请求,斜纹和饼干,它们似乎都没有用。 用户帐户列表如下:
username: zhonghao007
password: zonghao0915
登录成功后,您可以访问其他网站,在http://www.ppdai.com/account1/lend.aspx
查看您的个人信息感谢您的帮助 这是请求代码
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()# Get local session of firefox
browser.get("http://www.ppdai.com/login.aspx") # Load page
userid = browser.find_element_by_id('txtUserName')
userid.send_keys('zhonghao007')
passw = browser.find_element_by_id('txtPassword')
passw.send_keys('zonghao0915')
signin = browser.find_element_by_id('btnLogin')
signin.click()
答案 0 :(得分:1)
查看该页面的源代码,您可以看到该表单具有以下结构:
<form name="form1" method="post" action="login.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTM0MDY0ODA1OQ8WAh4LVXJsUmVmZXJyZXIFVGh0dHA6Ly9zdGFja292ZXJmbG93LmNvbS9xdWVzdGlvbnMvMTYxMjk0MzEvbG9naW4tdG8tYS1zcGVjaWZpYy13ZWJzaXRlLXVzaW5nLXB5dGhvbhYCAgEPFgIeBFRleHQF7gE8c2NyaXB0IHNyYz0iaHR0cDovL3d3dy5nb29nbGUtYW5hbHl0aWNzLmNvbS9nYS5qcyIgdHlwZT0idGV4dC9qYXZhc2NyaXB0Ij48L3NjcmlwdD48c2NyaXB0IHR5cGU9InRleHQvamF2YXNjcmlwdCI+dHJ5IHt2YXIgcHQgPSBfZ2F0Ll9nZXRUcmFja2VyKCJVQS0zNjM1NjY0LTEiKTtwdC5fc2V0RG9tYWluTmFtZSgnLnBwZGFpLmNvbScpO3B0Ll90cmFja1BhZ2V2aWV3KCk7fSBjYXRjaChlcnIpIHt9PC9zY3JpcHQ+ZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUIYnRuTG9naW7FDjuchG1Ou3OnbNrC9L5mLwpESA==" />
<input type="hidden" name="__EVENTVALIDATION" value="/wEWBQLh9ro4AqXVsrMJArWptJELAoLch4YMApCjwqsN3xZOGR6YaITqg9kNkVxXG+bnVLo=" />
<input name="txtUserName" type="text" >
<input name="txtPassword" type="password" />
<input type="image" name="btnLogin" id="btnLogin" />
<input type="hidden" name="HiddenField1" id="HiddenField1" value="http://stackoverflow.com/questions/16129431/login-to-a-specific-website-using-python" />
</form>
因此,您应该可以使用以下数据对http://www.ppdai.com/login.aspx进行POST
r = requests.post('http://www.ppdai.com/login.aspx', data={
'__VIEWSTATE': '/wEPDwUKMTM0MDY0ODA1OQ8WAh4LVXJsUmVmZXJyZXIFVGh0dHA6Ly9zdGFja292ZXJmbG93LmNvbS9xdWVzdGlvbnMvMTYxMjk0MzEvbG9naW4tdG8tYS1zcGVjaWZpYy13ZWJzaXRlLXVzaW5nLXB5dGhvbhYCAgEPFgIeBFRleHQF7gE8c2NyaXB0IHNyYz0iaHR0cDovL3d3dy5nb29nbGUtYW5hbHl0aWNzLmNvbS9nYS5qcyIgdHlwZT0idGV4dC9qYXZhc2NyaXB0Ij48L3NjcmlwdD48c2NyaXB0IHR5cGU9InRleHQvamF2YXNjcmlwdCI+dHJ5IHt2YXIgcHQgPSBfZ2F0Ll9nZXRUcmFja2VyKCJVQS0zNjM1NjY0LTEiKTtwdC5fc2V0RG9tYWluTmFtZSgnLnBwZGFpLmNvbScpO3B0Ll90cmFja1BhZ2V2aWV3KCk7fSBjYXRjaChlcnIpIHt9PC9zY3JpcHQ+ZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUIYnRuTG9naW7FDjuchG1Ou3OnbNrC9L5mLwpESA==',
'__EVENTVALIDATION': '/wEWBQLh9ro4AqXVsrMJArWptJELAoLch4YMApCjwqsN3xZOGR6YaITqg9kNkVxXG+bnVLo='
'txtUserName': 'zhonghao007',
'txtPassword': 'zonghao0915',
'HiddenField1': 'http://www.somereferring-url.com'
}
)
viewstate和eventvalidation看起来像是每个页面请求生成的,并且可能用于某种CSRF验证。因此,您可能需要首先请求页面,找到这些字段的值,然后使用新生成的请求发出POST请求。您还需要确保传递会话cookie。
或者,您可以使用Selenium webdriver直接驱动真正的浏览器。这意味着它肯定会奏效。