Cookie to log in with Jsoup?

后端 未结 3 1779
忘掉有多难
忘掉有多难 2021-01-15 02:24

For a project I\'m trying to get data from a website only acessible when you\'re logged in from the site Goodreads.com. I\'m new to Jsoup, since I\'m using it only for this

3条回答
  •  鱼传尺愫
    2021-01-15 02:43

    You can log in with this code:

    public static void main(String[] args) throws Exception {
    
        Connection.Response execute = Jsoup
                .connect("https://www.goodreads.com/")
                .method(Connection.Method.GET).execute();
    
        Element sign_in = execute.parse().getElementById("sign_in");
        String authenticityToken = sign_in.select("input[name=authenticity_token]").first().val();
        String n = sign_in.select("input[name=n]").first().val();
    
        Document document = Jsoup.connect("https://www.goodreads.com/user/sign_in")
                .data("cookieexists", "✓")
                .data("authenticity_token", authenticityToken)
                .data("user[email]", "user@email.com")
                .data("user[password]", "password")
                .data("remember_me", "on")
                .data("n", n)
                .cookies(execute.cookies())
                .post();
    
    }
    

提交回复
热议问题