jsoup发布和cookie

Gwi*_*dow 49 java screen-scraping jsoup

我正在尝试使用jsoup登录网站,然后抓取信息,我遇到问题,我可以成功登录并从index.php创建一个文档,但我无法在网站上获取其他页面.我知道我需要在发布后设置一个cookie,然后在我尝试在网站上打开另一个页面时加载它.但是我该怎么做?以下代码允许我登录并获取index.php

Document doc = Jsoup.connect("http://www.example.com/login.php")
               .data("username", "myUsername", 
                     "password", "myPassword")
               .post();
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用apache httpclient来做到这一点,但我不想这样做.

Jon*_*ley 100

当您登录该站点时,可能会设置一个授权会话cookie,需要在后续请求中发送以维护会话.

您可以像这样获取cookie:

Connection.Response res = Jsoup.connect("http://www.example.com/login.php")
    .data("username", "myUsername", "password", "myPassword")
    .method(Method.POST)
    .execute();

Document doc = res.parse();
String sessionId = res.cookie("SESSIONID"); // you will need to check what the right cookie name is
Run Code Online (Sandbox Code Playgroud)

然后在下一个请求发送它,如:

Document doc2 = Jsoup.connect("http://www.example.com/otherPage")
    .cookie("SESSIONID", sessionId)
    .get();
Run Code Online (Sandbox Code Playgroud)


Igo*_*tos 19

//This will get you the response.
Response res = Jsoup
    .connect("loginPageUrl")
    .data("loginField", "login@login.com", "passField", "pass1234")
    .method(Method.POST)
    .execute();

//This will get you cookies
Map<String, String> loginCookies = res.cookies();

//And this is the easiest way I've found to remain in session
Document doc = Jsoup.connect("urlYouNeedToBeLoggedInToAccess")
      .cookies(loginCookies)
      .get();
Run Code Online (Sandbox Code Playgroud)