使用Jsoup解析HTML和文档

时间:2022-10-31 09:24:08

      对不需要登陆的网页或者已经获得到的文档的解析可以使用Jsoup,Jsoup提供了相应的API供我们使用。

    Jsoup的官方文档 http://www.open-open.com/jsoup/

    下面是Jsoup的操作,使用见注释

 

  

package Jsoup_SWPU;

import java.io.File;
import java.io.IOException;

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 org.jsoup.select.Evaluator.Id;

public class Spider {
public static void main(String[] args) throws IOException {
Validate.isTrue(args.length == 1, "usage: supply url to fetch");
String url = args[0];
print("Fetching %s...", url);
try {
Document doc = Jsoup.connect(url).get();
/*
* File in = new File("F:/hao123.html"); Document doc =
* Jsoup.parse(in, "UTF-8", "http://www.hao123.com/");
*/

// Elements link = doc.select("a[href]");
Elements links = doc.select("a[href]"); // a是标签,href是属性,属性用方括号
Elements element = doc.select("div");
Elements media = doc.select("img[src]");
Elements imports = doc.select("link[href]");
Elements classElements = doc.select("div[class]"); // select()返回的是Elements,再对它里的属性进行检索
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)); //attr获取属性值,也可以给属性赋值 else
* print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
*/
}

print("\nDiv", element.size());

for (Element divElement : element) {
// print(" * %s <%s> (%s)",
// divElement.tagName(),divElement.attr("id"),
// divElement.attr("class"));
}

print("\nImports: (%d)", imports.size());
for (Element link : imports) {
print(" * %s <%s> (%s)", link.tagName(), link.attr("abs:href"),
link.attr("rel"));
}

print("\nLinks: (%d)", links.size());
print("test a link:<%s>\n", links.last().attr("abs:href"));
print("the next page:<%s>",
Jsoup.connect(links.last().attr("abs:href")).get().text());
for (Element link : links) {
// print(" * a: <%s> (%s)", link.attr("abs:href"),
// trim(link.text(), 35));
// print("data is :{%s} ", link.data());
// print("html is:[%s]", link.html());

}

print("\nClasses:(%d)", classElements.size());
for (Element classes : classElements) {
// print(" * c :<%s> (%s)",
// classes.attr("class"),classes.getElementsByIndexEquals(0).text());
} // 获取它的内容
System.out
.println("link size is:" + links.size()
+ ";import size is:" + imports.size()
+ ";media size is:" + media.size()
+ ";classes size is:" + classElements.size());

} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
}

private static void print(String msg, Object... args) {
System.out.println(String.format(msg, args));
}

private static String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width - 1) + ".";
else
return s;
}

}

 

要注意的是元素