为什么请求没有正确登录网站? [英] why isn't Requests not signing into a website correctly?

查看:76
本文介绍了为什么请求没有正确登录网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用请求库登录linkedin.在查看完成此操作的最佳方法后,尝试使用requests.Session()进行此操作,但未成功. 我相信这与我发布的链接有关.

I am trying to sign into linkedin by using the requests library. 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. 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()

推荐答案

我首先要说明您确实应该使用他们的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')

这篇关于为什么请求没有正确登录网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆