且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么请求没有正确登录网站?

更新时间:2023-01-02 15:51:23

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

I should start with stating that you really should use their API: http://developer.linkedin.com/apis

使用这些参数的linkedin首页上似乎没有任何POST登录信息?

There does not seem to be any POST login on the frontpage of linkedin using those parameters?

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

This is the login URL you must POST to: https://www.linkedin.com/uas/login-submit

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

Be aware that this probably wont work either, as you need at least the csrfToken parameter from the login form.

您可能也需要loginCsrfParam,同样也可以从首页的登录表单中获得.

You probably need the loginCsrfParam too, also from the login form on the frontpage.

类似的事情可能会起作用.未经测试,您可能需要添加其他POST参数.

Something like this might work. Not tested, you might need to add the other POST parameters.

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')