首页 > 代码库 > jsoup: Java HTML 解析器

jsoup: Java HTML 解析器

jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。

请参考:jsoup.org

例如: beanshell 抓取CSDN极客头条内容  soup.bsh

// 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;

/**
 * closure program to list links from a URL.
 */
ListLinks()
{
    run(String url){
        if(url.length() <10) return;
        print(url);

        Document doc = Jsoup.connect(url).get();
        Elements links = doc.select("h4 a"); // direct a after h4
 
        print("Links: "+ links.size() +"\n");
        for (Element link : links) {
            print(link.attr("abs:href"));
			print(link.text());
        }
    }
    return this;
}

lslinks = ListLinks();
lslinks.run("http://geek.csdn.net/hot");
cmd 运行 java bsh.Interpreter soup.bsh