Example usage for org.jsoup.nodes Document select

List of usage examples for org.jsoup.nodes Document select

Introduction

In this page you can find the example usage for org.jsoup.nodes Document select.

Prototype

public Elements select(String cssQuery) 

Source Link

Document

Find elements that match the Selector CSS query, with this element as the starting context.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Document doc = Jsoup.parse(new File("t2.html"), "UTF-8");
    doc.select("area#area1").after("<area id=\"newArea\" />");
    System.out.println(doc.html());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String URL = "http://www.your server.com";
    Document doc = Jsoup.connect(URL).get();

    String cheapest = doc.select("b.item-price").first().text();
    System.out.println(cheapest);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String URL = "http://www.your server.com";
    Document doc = Jsoup.connect(URL).get();

    String cheapest = doc.select("input[name=p-min]").first().attr("value");
    System.out.println(cheapest);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String html = "<description>" + "..." + "</description>";

    Document doc = Jsoup.parse(html);
    for (Element desc : doc.select("description")) {
        String unescapedHtml = desc.text();
        String src = Jsoup.parse(unescapedHtml).select("img").first().attr("src");
        System.out.println(src);/*from   w w  w .j  a  va 2 s .c o m*/
    }
    System.out.println("Done");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xmlStr = "<style>" + //
            "v\\:* {behavior:url(#default#VML);}" + //
            "o\\:* {behavior:url(#default#VML);}" + //
            "w\\:* {behavior:url(#default#VML);}" + //
            ".shape {behavior:url(#default#VML);}" + //
            "</style>" + //
            "<xml>" + //
            "<w:WordDocument>" + //
            "<w:View>Normal</w:View>" + //
            "<w:Zoom>0</w:Zoom>" + //
            "<w:TrackMoves>false</w:TrackMoves>" + //
            "</xml>";
    Document doc = Jsoup.parse(xmlStr, "", Parser.xmlParser());
    doc.select("style").remove();
    System.out.println(doc);//from www  . j  a va 2 s .  c  o  m
}

From source file:Main.java

public static void main(String[] args) {
    String html = "<html>                        " + "  <body>                      "
            + "    <table id='myTable'>      " + "      <tbody>                 "
            + "        <tr>                  " + "          <th>header</th>     "
            + "          <td>1</td>          " + "            <table>           "
            + "              <tbody>         " + "                <tr>          "
            + "                  <td>a1</td> " + "                  <td>a2</td> "
            + "                  <td>a3</td> " + "                </tr>         "
            + "              </tbody>        " + "            </table>          "
            + "          </td>               " + "          <td>high level2</td>"
            + "          <td>high level3</td>" + "        </tr>                 "
            + "      </tbody>                " + "    </table>                  "
            + "  </body>                     " + "</html>                       ";
    Document doc = Jsoup.parse(html);
    Elements highLevelTDs = doc.select("#myTable > tbody > tr > td");
    System.out.println(highLevelTDs.size());
    for (Element td : highLevelTDs) {
        System.out.println(td);//from ww  w. ja  v a  2s .  c o  m
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    String url = "data.html";
    Document doc = Jsoup.parse(new File(url), "UTF-8");
    Elements rows = doc.select("tr");
    for (Element row : rows) {
        print("---------");
        Elements data = row.getElementsByTag("td");
        print("First Name:%s", data.get(0).text());
        print("Last Name:%s", data.get(1).text());
        print("Date:%s", data.get(2).text());
        print("City:%s", data.get(3).text());
    }//ww  w . j  a  v a  2  s .c o m
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    Document doc = Jsoup.connect("http://www.your server.com/").get();
    Elements tableRows = doc.select("tr");
    for (Element row : tableRows) {
        Elements cls1 = row.getElementsByClass("cls1");
        Elements cls2 = row.getElementsByClass("cls2");
        Elements cls3 = row.getElementsByClass("cls3");

        if (!cls1.isEmpty() && !cls2.isEmpty() && !cls3.isEmpty()) {
            System.out.println(cls1.get(0).text());
            System.out.println(cls2.get(0).text());
            System.out.println(cls3.get(0).text());
        }/*w  w  w .  jav  a 2  s  .  c o  m*/
    }
}

From source file:Main.java

public static void main(String[] args) {

    Document doc = Jsoup.parse("html with frame", "UTF-8");
    Document noFramesDoc = Jsoup.parseBodyFragment(doc.select("noframes").text());
    System.out.println("Age = " + noFramesDoc.select("input[id=age]").attr("value"));
    System.out.println("Class = " + noFramesDoc.select("input[id=class]").attr("value"));

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    String docId = "1";
    String url = "http://www.your server.com/" + docId;
    Document doc = Jsoup.connect(url).timeout(60000).userAgent("Mozilla/25.0").get();
    Elements authors = doc.select("div");

    System.out.println("authors=" + authors);
    System.out.println("authors.length=" + authors.size());

    for (Element a : authors) {
        System.out.println("  author: " + a);
    }/*  w  ww. ja  va2  s.c  om*/
}