如何使用jsoup创建会话以及如何使用jsoup发布数据

san*_*ank 3 jsoup

我无法使用jsoup创建会话以及如何使用发布数据jsoup.请帮助我,我是新手jsoup api,实际上我的代码是:

Connection.Response res = Jsoup.connect("https://wiki.my---------------")
    .userAgent("Mozila")
    .timeout(0)
    .method(Method.GET)
    .execute();

Document docu = res.parse();
Map<String, String> cookies = afterLogin.cookies();
Document doc2 = (Document) Jsoup.connect("https://wiki.my------------------")
    .data("os_username", "A57", "os_password", "pass")
    .data("login", "Log on")
    .cookies(cookies)
    .timeout(0)
    .post();
Run Code Online (Sandbox Code Playgroud)

我得到一个网页(doc2),然后添加到该网页的某个表(doc2)?

如何添加新数据已经存在的网页doc2,然后如何将doc2发布到另一个url.已经尝试了很多,请帮助我.

Mar*_*szS 5

也许你使用错误的cookie,试试这个

Document doc2 = (Document) Jsoup.connect("https://wiki.myatos.net/display/RMSTRV/CCBO+Patches")
    .data("os_username", "A57", "os_password", "pass")
    .data("login", "Log on")
    .cookies(res.cookies())
    .timeout(0)
    .post();
Run Code Online (Sandbox Code Playgroud)


use*_*337 5

要使用jsoup登录任何网站并从本网站检索和解析数据,您只需按照以下说明操作即可:

首先,您需要知道您将使用它来登录网站所有页面的cookie名称(会话名称).

注意:您可以知道任何网站的会话名称如下:登录您想知道会话名称的网站或其中的cookie然后在URL字段中写入此命令

javascript:void(alert(document.cookie))
Run Code Online (Sandbox Code Playgroud)

复制会话名称然后执行此操作

Response res = Jsoup.connect("login Site URL")
            .method(Method.GET)
            .timeout(10000)
            .execute();

   String sessionID = res.cookie("SESSION ID for site");//here put the Session Name for website 
Run Code Online (Sandbox Code Playgroud)

现在您拥有网站的会话名称,但仍需要填写您的登录信息

String username="your username";
String password="your pass";

Jsoup.connect("login Site URL")
            .data("login:username", username, "login:password", password, "login:loginImg", "", "login", "login")
            .cookie("SESSIONID", sessionID)
            .method(Method.POST)
            .timeout(10000)
            .execute();// now you have filled the Session Name with your login info and you can use it for any page in website


Document doc = Jsoup.connect("any page of site")
            .cookie("SESSIONID", sessionID)
            .timeout(10000)
            .get();// her to open any page with Session 
Run Code Online (Sandbox Code Playgroud)

你已经拥有它的名字

现在您只需要转到从中获取数据所需的标记或元素

System.out.println(doc.body().text());// for print all text in page
Run Code Online (Sandbox Code Playgroud)

或者如果您有元素ID,您可以获得这样的数据

String S = doc.select("span[id^=here put element id]");
System.out.println(S);
Run Code Online (Sandbox Code Playgroud)

或者像这样

String S=doc.getElementById("ElementID").text();
System.out.println(S);
Run Code Online (Sandbox Code Playgroud)