jsoup小例子

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class FirstExtract {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		parseHtml();		
		parseBody();
		parseUrl();		
		navigation();		
		extractElement();
		*/
		
		navigation();
		}
	
	public static void parseHtml()
	{
		System.out.println("Print the html --------------------");
		String html = "<html><head><title>First parse</title></head>"
				  + "<body><p>Parsed HTML into a doc.</p></body></html>";
		Document doc = Jsoup.parse(html);		
		System.out.println(doc);
		System.out.println("Print the html head --------------------");
		System.out.println(doc.head());
		System.out.println("Print the html body --------------------");
		System.out.println(doc.body());
		System.out.println("Print the html title --------------------");
		System.out.println(doc.title());
	}
	
	public static void parseBody()
	{
		String html = "<div><p>Lorem ipsum.</p>";
		Document doc = Jsoup.parseBodyFragment(html);
		Element body = doc.body();	
		System.out.println("Print the body --------------------");
		System.out.println(body);
	}
	
	public static void parseUrl()
	{
		try {
			Document doc = Jsoup.connect("http://book.sina.cn/prog/wapsite/books/h5/vipc.php?bid=232872&cid=343466&cp=1&sort=asc&PHPSESSID=bd8c678e4c42e771dbaa3ac125aaf009&vt=4").get();
			System.out.println("Print the Url --------------------");
			System.out.println(doc);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void navigation()
	{
		String html="<html><head><title>First parse</title></head>"
				  + "<body><p>Parsed HTML into a doc.</p><div id=content>"
				  + "<a href='/hahaha.html'>hahaha</a>"
				  + "<a href='bababa.html'>bababa</a>"
				  +"</div></body></html>";
		Document doc = Jsoup.parse(html, "http://192.168.3.84/gamestore/index.html");
		
		Element content = doc.getElementById("content");
		Elements links = content.getElementsByTag("a");
		for (Element link : links) {
		  String linkHref = link.attr("href");
		  String linkAbsHref = link.absUrl("href");
		  String linkText = link.text();
		  
		  System.out.println(linkHref);
		  System.out.println(linkAbsHref);
		  System.out.println(linkText);
		}

	}
	
	public static void extractElement()
	{
		String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
		Document doc = Jsoup.parse(html);
		Element link = doc.select("a").first();

		String text = doc.body().text(); // "An example link"
		String linkHref = link.attr("href"); // "http://example.com/"
		String linkText = link.text(); // "example""

		String linkOuterH = link.outerHtml(); 
		    // "<a href="http://example.com"><b>example</b></a>"
		String linkInnerH = link.html(); // "<b>example</b>"
		
		System.out.println(text);
		System.out.println(linkHref);
		System.out.println(linkText);
		System.out.println(linkOuterH);
		System.out.println(linkInnerH);
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值