简体   繁体   English

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

[英]why isn't Requests not signing into a website correctly?

I am trying to sign into linkedin by using the requests library. 我正在尝试使用请求库登录linkedin。 After looking around the best way to do this is with using a requests.Session() I attempted to do this, but I was not successful. 在查看完成此操作的最佳方法之后,就是使用了requests.Session()我尝试执行此操作,但未成功。 I believe it has something to do with the link I post to. 我相信这与我发布的链接有关。

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

I should start with stating that you really should use their API: http://developer.linkedin.com/apis 我首先要说明您确实应该使用他们的API: http : //developer.linkedin.com/apis

There does not seem to be any POST login on the frontpage of linkedin using those parameters? 使用这些参数的linkedin首页似乎没有任何POST登录信息?

This is the login URL you must POST to: https://www.linkedin.com/uas/login-submit 这是您必须发布到的登录URL: https : //www.linkedin.com/uas/login-submit

Be aware that this probably wont work either, as you need at least the csrfToken parameter from the login form. 请注意,这可能也不起作用,因为您至少需要登录表单中的csrfToken参数。

You probably need the loginCsrfParam too, also from the login form on the frontpage. 您可能也需要loginCsrfParam,同样也可以从首页的登录表单中获得。

Something like this might work. 这样的事情可能会起作用。 Not tested, you might need to add the other POST parameters. 未经测试,您可能需要添加其他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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM