JSOUP - How to crawl a “login required” page using JSOUP

前端 未结 1 391
醉话见心
醉话见心 2021-01-22 12:34

I\'m having trouble at crawling a determined website I wish to crawl. The problem is: after successfully logging in to that website I can\'t access a link which requires a valid

相关标签:
1条回答
  • 2021-01-22 12:41

    Get the cookie after you login:

        Connection.Response loginForm = Jsoup.connect(url)
                .method(Connection.Method.GET)
                .execute();
    
        Connection.Response mainPage = Jsoup.connect(login-validation-url)
                .data("user", user)
                .data("senha", password)
                .cookies(loginForm.cookies())
                .execute();
    
        Map<String, String> cookies = mainPage.cookies();
    
        Document evaluationPage = Jsoup.connect(login-required-url)
                .cookies(cookies)
                .execute.parse();
    
       return evaluationPage;
    

    When you get your second webpage, you also have to use the cookie:

    (Source: I had this problem a few days ago)

    So it's easier to just put the cookies in a Map:

    Map<String, String> cookies = loginForm.cookies();
    

    And submit the forms using these cookies.

    0 讨论(0)
提交回复
热议问题