为什么请求不能正确登录到网站?

2024-04-18 23:22:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过使用请求库登录linkedin。在环顾了这一点的最佳方法之后,我尝试使用^{cd1>}来完成这一点,但我没有成功。 我相信这与我发到的链接有关。

import requests

payload = {
    'session_key': EMAIL_GOES_HERE,
    'session_password': PASSWORD_GOES_HERE
}

with requests.Session() as s:
    s.post('https://www.linkedin.com/', data=payload)
#program should be signed in here so I am going onto a private page that requeires the user to be signed in.
r=s.get('https://www.linkedin.com/vsearch/p?f_CC=2289109')
#saving the results in an HTML file for easy debugging/viewing 
html= open('testtest.html', 'w')
html.write(r.content)
html.close()

Tags: theinhttpscomheresessionhtmlwww
1条回答
网友
1楼 · 发布于 2024-04-18 23:22:12

我应该首先说明您确实应该使用他们的API: http://developer.linkedin.com/apis

在linkedin的首页上似乎没有使用这些参数的POST登录?在

这是您必须发布到的登录URL: https://www.linkedin.com/uas/login-submit

请注意,这可能也不起作用,因为您至少需要登录表单中的csrfToken参数。在

您可能还需要loginCsrfParam,也可以从frontpage的登录表单中获取。在

像这样的事情也许会奏效。未经测试,您可能需要添加其他POST参数。在

import requests
s = requests.session()

def get_csrf_tokens():
    url = "https://www.linkedin.com/"
    req = s.get(url).text

    csrf_token = req.split('name="csrfToken" value=')[1].split('" id="')[0]
    login_csrf_token = req.split('name="loginCsrfParam" value="')[1].split('" id="')[0]

    return csrf_token, login_csrf_token


def login(username, password):
    url = "https://www.linkedin.com/uas/login-submit"
    csrfToken, loginCsrfParam = get_csrf_tokens()

    data = {
        'session_key': username,
        'session_password': password,
        'csrfToken': csrfToken,
        'loginCsrfParam': loginCsrfParams
    }

    req = s.post(url, data=data)

login('username', 'password')

相关问题 更多 >