Python使用请求登录网站

时间:2017-07-13 06:10:51

标签: python web-scraping web-crawler python-requests bots

def login_form(login_url,  passkey):
    html = requests.get(login_url)
    response = requests.post(login_url, data = passkey, cookies = 
    html.cookies)
    return response


passkey = { "username": "pass", "password": "password"}


login_info = login_form('https://www.cpaelites.com/member.php?action=login', 
  passkey)
print(login_info.url)

返回URL响应与登录相同。但它没有显示任何错误 我尝试在cpaelites上自动执行登录过程,我没有显示任何错误,但是当我打印响应URL时,它显示我仍然在当前页面,这意味着我没有登录成功

1 个答案:

答案 0 :(得分:0)

您正在data传递凭据,您可以尝试使用auth参数

from requests.auth import HTTPBasicAuth
def login_form(login_url,  passkey):
    html = requests.get(login_url)
    response = requests.post(login_url, auth = HTTPBasicAuth(passkey['username'], passkey['password']), cookies = html.cookies)
    return response


passkey = { "username": "pass", "password": "password"}


login_info = login_form('https://www.cpaelites.com/member.php?action=login', passkey)
print(login_info.url)