Example usage for org.jsoup.nodes Element getElementsByTag

List of usage examples for org.jsoup.nodes Element getElementsByTag

Introduction

In this page you can find the example usage for org.jsoup.nodes Element getElementsByTag.

Prototype

public Elements getElementsByTag(String tagName) 

Source Link

Document

Finds elements, including and recursively under this element, with the specified tag name.

Usage

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());
    }/* w ww . j av a 2 s  .co  m*/
}

From source file:Main.java

public static Elements getAllElementsByType(Element elem, String type) {
    return elem.getElementsByTag(type);
}

From source file:io.github.carlomicieli.footballdb.starter.parsers.TeamRosterParser.java

private static Optional<Element> rosterTableBody(Element table) {
    return table.getElementsByTag("tbody").stream().skip(1).limit(1).findFirst();
}

From source file:io.github.carlomicieli.footballdb.starter.parsers.TeamRosterParser.java

private static Stream<Element> rosterTableRows(Element tableBody) {
    return tableBody.getElementsByTag("tr").stream();
}

From source file:com.gistlabs.mechanize.document.html.JsoupDataUtil.java

private static Element findFirstByTag(Element current, String[] tags, int index) {
    if (index < tags.length) {
        Elements elements = current.getElementsByTag(tags[index]);
        for (Element element : elements) {
            Element result = findFirstByTag(element, tags, index + 1);
            if (result != null)
                return result;
        }//  w w w .j a  v  a  2  s.c  o m
        return null;
    } else
        return current;
}

From source file:eu.masconsult.bgbanking.banks.procreditbank.ProcreditClient.java

private static RawBankAccount obtainBankAccountFromHtmlTableRow(Element row) {
    Elements cells = row.getElementsByTag("td");
    // if this is the header, skip it
    if (cells.size() == 0) {
        return null;
    }/*  w  w w  . j  a v  a 2 s  .  c om*/

    return new RawBankAccount().setServerId(cells.get(0).text())
            .setName(Convert.formatIBAN(cells.get(0).text())).setIBAN(cells.get(0).text())
            .setCurrency(cells.get(1).text()).setBalance(Convert.strToFloat(cells.get(4).text()))
            .setAvailableBalance(Convert.strToFloat(cells.get(5).text()));
}

From source file:com.geecko.QuickLyric.lyrics.LyricWiki.java

@Reflection
public static ArrayList<Lyrics> search(String query) {
    ArrayList<Lyrics> results = new ArrayList<>();
    query = query + " song";
    query = Normalizer.normalize(query, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+",
            "");//from w w  w.ja  va 2 s.  c  o  m
    try {
        URL queryURL = new URL(String.format(baseSearchUrl, URLEncoder.encode(query, "UTF-8")));
        Document searchpage = Jsoup.connect(queryURL.toExternalForm()).get();
        Elements searchResults = searchpage.getElementsByClass("Results");
        if (searchResults.size() >= 1) {
            searchResults = searchResults.get(0).getElementsByClass("result");
            for (Element searchResult : searchResults) {
                String[] tags = searchResult.getElementsByTag("h1").text().split(":");
                if (tags.length != 2)
                    continue;
                String url = searchResult.getElementsByTag("a").attr("href");
                Lyrics lyrics = new Lyrics(SEARCH_ITEM);
                lyrics.setArtist(tags[0]);
                lyrics.setTitle(tags[1]);
                lyrics.setURL(url);
                lyrics.setSource(domain);
                results.add(lyrics);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return results;
}

From source file:org.brnvrn.Main.java

/**
 * Parse a tr HTML element describing the tool
 * @param tool is to be updated/*from w w w.ja va  2  s .c  o m*/
 * @param tr   brings the data
 * @return true if successful
 */
private static boolean parseTrTool(Tool tool, Element tr) {
    boolean success = true;

    Element nameLink = tr.select("td:eq(0)").first();
    if (nameLink == null)
        return false;
    tool.setName(nameLink.text());
    tool.setUrl(nameLink.getElementsByTag("a").attr("href"));

    tool.setLicense(tr.select("td:eq(2)").first().text());

    tool.setCompatibility(tr.select("td:eq(3)").first().text());

    // More complicated: We will extract and remove known nodes, the rest will be description
    Element tdDescription = tr.select("td:eq(1)").first();
    Elements smalls = tdDescription.getElementsByTag("small");
    for (Element small : smalls) {
        Element author = small.getElementsContainingText("Author").first();
        if (author != null) {
            String authorsString = author.text();
            authorsString = authorsString.substring(authorsString.indexOf(":") + 1);
            tool.addAuthor(authorsString.split(","));
            small.remove();
        }
        Element sourceCode = small.getElementsContainingText("ource").last();
        if (sourceCode != null) {
            tool.setUrl_src(sourceCode.attr("href"));
            small.remove();
        }
    }
    tdDescription.getElementsByTag("br").remove();
    tool.setDescription(Jsoup.clean(tdDescription.html(), Whitelist.relaxed())); // ownText will miss the contained links in the description
    tool.setDescriptionText(tdDescription.text());

    bestEffortThemeLanguage(tool);

    return success;
}

From source file:org.keycloak.testsuite.util.saml.LoginBuilder.java

public static HttpUriRequest handleLoginPage(UserRepresentation user, String loginPage) {
    String username = user.getUsername();
    String password = getPasswordOf(user);
    org.jsoup.nodes.Document theLoginPage = Jsoup.parse(loginPage);

    List<NameValuePair> parameters = new LinkedList<>();
    for (Element form : theLoginPage.getElementsByTag("form")) {
        String method = form.attr("method");
        String action = form.attr("action");
        boolean isPost = method != null && "post".equalsIgnoreCase(method);

        for (Element input : form.getElementsByTag("input")) {
            if (Objects.equals(input.id(), "username")) {
                parameters.add(new BasicNameValuePair(input.attr("name"), username));
            } else if (Objects.equals(input.id(), "password")) {
                parameters.add(new BasicNameValuePair(input.attr("name"), password));
            } else {
                parameters.add(new BasicNameValuePair(input.attr("name"), input.val()));
            }//from  ww w  .j  a v a2s.c  o m
        }

        if (isPost) {
            HttpPost res = new HttpPost(action);

            UrlEncodedFormEntity formEntity;
            try {
                formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            res.setEntity(formEntity);

            return res;
        } else {
            UriBuilder b = UriBuilder.fromPath(action);
            for (NameValuePair parameter : parameters) {
                b.queryParam(parameter.getName(), parameter.getValue());
            }
            return new HttpGet(b.build());
        }
    }

    throw new IllegalArgumentException("Invalid login form: " + loginPage);
}

From source file:app.sunstreak.yourpisd.net.Parser.java

/**
 * /*w  w w . j  a  va 2 s  . c  om*/
 * @return
 */
public static int getClassId(Element row) {
    Elements th = row.getElementsByTag("th");
    String href = th.get(0).getElementsByTag("a").get(0).attr("href");
    // href="javascript:ClassDetails.getClassDetails(2976981);"
    return Integer.parseInt(href.substring(40, 47));
}