A handy parsing HTML tool--JSOUP__JS

Source: Internet
Author: User
Tags abs tagname


Using the syntax like jquery, it's easy to use.

Java Sample code: http://www.open-open.com/jsoup/example-list-links.htm

Package org.jsoup.examples;
Import Org.jsoup.Jsoup;
Import Org.jsoup.helper.Validate;
Import org.jsoup.nodes.Document;
Import org.jsoup.nodes.Element;

Import org.jsoup.select.Elements;

Import java.io.IOException;
 /** * Example program to list links from a URL. */public class Listlinks {public static void main (string[] args) throws IOException {validate.istrue (ARGS.L
        Ength = = 1, "Usage:supply URL to fetch");
        String URL = args[0];

        Print ("Fetching%s ...", url);
        Document doc = jsoup.connect (URL). get ();
        Elements links = doc.select ("a[href]");
        Elements media = doc.select ("[src]");

        Elements imports = Doc.select ("link[href]");
        Print ("\nmedia: (%d)", media.size ()); for (Element Src:media) {if (Src.tagname (). Equals ("img")) Print ("*%s: <%s>%sx%s" (%
                       s) ", Src.tagname (), Src.attr (" Abs:src "), src.attr (" width "), src.attr (" height "), Trim (src.attr ("Alt"), 20));
        else print ("*%s: <%s>", Src.tagname (), Src.attr ("abs:src"));
        Print ("\nimports: (%d)", imports.size ()); for (Element link:imports) {print ("*%s <%s> (%s)", Link.tagname (), Link.attr ("Abs:href"), Link.attr (
        Print ("\nlinks: (%d)", links.size ());
        for (Element link:links) {print ("* A: <%s> (%s)", Link.attr ("Abs:href"), Trim (Link.text (), 35)); }} private static void print (String msg, Object ... args) {System.out.println (msg,
    args)); private static String trim (string s, int width) {if (S.length () > width) return s.substring
        (0, Width-1) + ".";
    else return s; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.