无法使用Python请求登录Linkedin

时间:2019-02-05 08:02:39

标签: python login python-requests linkedin

我使用Python请求登录到LinkedIn,但是,我的代码不起作用,也没有弄清楚原因,这是我的代码行:

url_2 = "https://www.linkedin.com"
headers = {
        'Content-Type': "application/x-www-form-urlencoded",
        'Host': "www.linkedin.com",
        'Origin': "https://www.linkedin.com",
        'User-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
    }

soup = BeautifulSoup(requests.session().get(url_2).text, "html.parser")

payload_2 = {
    "session_key": "myemail",
    "session_password": "mypwd",
    "isJsEnabled": "false"
}

ck = dict(requests.session().get(url_2).cookies)

payload_2["loginCsrfParam"] = soup.find(id="loginCsrfParam-login")['value']

result_2 = requests.session().post(
    soup.form['action'],
    data = payload_2,
    headers = headers,
    cookies = ck
)

url_3 = "https://www.linkedin.com/in/nhome/"
result_3 = requests.session().get(
    url_3,
    headers = headers
)

我弄错了什么吗?非常感谢!

这是我在查询result_3.text之后收到的输出:

'<html><head>\n<script type="text/javascript">\nwindow.onload = function() {\n  // Parse the tracking code from cookies.\n  var trk = "bf";\n  var trkInfo = "bf";\n  var cookies = document.cookie.split("; ");\n  for (var i = 0; i < cookies.length; ++i) {\n    if ((cookies[i].indexOf("trkCode=") == 0) && (cookies[i].length > 8)) {\n      trk = cookies[i].substring(8);\n    }\n    else if ((cookies[i].indexOf("trkInfo=") == 0) && (cookies[i].length > 8)) {\n      trkInfo = cookies[i].substring(8);\n    }\n  }\n\n  if (window.location.protocol == "http:") {\n    // If "sl" cookie is set, redirect to https.\n    for (var i = 0; i < cookies.length; ++i) {\n      if ((cookies[i].indexOf("sl=") == 0) && (cookies[i].length > 3)) {\n        window.location.href = "https:" + window.location.href.substring(window.location.protocol.length);\n        return;\n      }\n    }\n  }\n\n  // Get the new domain. For international domains such as\n  // fr.linkedin.com, we convert it to www.linkedin.com\n  var domain = "www.linkedin.com";\n  if (domain != location.host) {\n    var subdomainIndex = location.host.indexOf(".linkedin");\n    if (subdomainIndex != -1) {\n      domain = "www" + location.host.substring(subdomainIndex);\n    }\n  }\n\n  window.location.href = "https://" + domain + "/authwall?trk=" + trk + "&trkInfo=" + trkInfo +\n      "&originalReferer=" + document.referrer.substr(0, 200) +\n      "&sessionRedirect=" + encodeURIComponent(window.location.href);\n}\n</script>\n</head></html>'

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

import requests
from bs4 import BeautifulSoup

client = requests.Session()

HOMEPAGE_URL = 'https://www.linkedin.com'
LOGIN_URL = 'linkedin login URL'

html = client.get(HOMEPAGE_URL).content
soup = BeautifulSoup(html, "html.parser")
csrf = soup.find(id="loginCsrfParam-login")['value']

login_credentials = {
    'session_key':'Login',
    'session_password':'Password',
    'loginCsrfParam': csrf,
}

client.post(LOGIN_URL, data=login_credentials)

client.get(HOMEPAGE_URL)