使用JSoup登录Linkedin

Sor*_*ecu 5 java web-crawler linkedin jsoup

我需要使用Jsoup登录Linkedin,最好是.

这是我用来登录其他网站但它不适用于Linkedin.

Connection.Response res = Jsoup
    .connect("https://www.linkedin.com/uas/login?goback=&trk=hb_signin")
    .data("session_key", mail, "session_password", password)
    .method(Connection.Method.POST)
    .timeout(60000).

// Also tried "https://www.linkedin.com/uas/login-submit"

Map<String, String> loginCookies = res.cookies();
    //Checking a profile to see if it was succesful or if it returns the login page.    
Document currentPage = Jsoup.connect(someProfileLink).cookies(loginCookies).timeout(10000).
System.out.println("" + currentPage.text());
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

我需要能够通过使用网络爬虫来获取用户配置文件,但无论我尝试什么,我都无法获得登录cookie.

Ses*_*ius 3

您可以使用以下代码登录 Linkedin:

    try {

                String url = "https://www.linkedin.com/uas/login?goback=&trk=hb_signin";
                Connection.Response response = Jsoup
                        .connect(url)
                        .method(Connection.Method.GET)
                        .execute();

                Document responseDocument = response.parse();
                Element loginCsrfParam = responseDocument
                        .select("input[name=loginCsrfParam]")
                        .first();

                response = Jsoup.connect("https://www.linkedin.com/uas/login-submit")
                        .cookies(response.cookies())
                        .data("loginCsrfParam", loginCsrfParam.attr("value"))
                        .data("session_key", "your_login")
                        .data("session_password", "your_password")
                        .method(Connection.Method.POST)
                        .followRedirects(true)
                        .execute();

                Document document = response.parse();

    //            System.out.println(document)

                System.out.println("Welcome " 
                        + document.select(".act-set-name-split-link").html());

            } catch (IOException e) {
                e.printStackTrace();
            }
Run Code Online (Sandbox Code Playgroud)

  • @SorinGrecu要通过身份验证过程,您必须将会话cookie和CSRF令牌添加到您的POST请求中。请阅读[此处](http://stackoverflow.com/questions/5207160/what-is-a-csrf-token-what-is-its-importance-and-how-does-it-work)了解 CSRF 令牌。 (2认同)