在編程的時(shí)候或者寫網(wǎng)絡(luò)爬蟲的時(shí)候,經(jīng)常需要對(duì)html進(jìn)行解析,抽取其中有用的數(shù)據(jù)。一款好的工具是特別有用的,能提供很多的幫助,網(wǎng)上有很多這樣的工具,比如:htmlcleaner、htmlparser
經(jīng)使用比較:感覺 htmlcleaner 比 htmlparser 好用,尤其是htmlcleaner 的 xpath特好用。
下面針對(duì)htmlcleaner進(jìn)行舉例說明,需求為:取出title,name=”my_href” 的鏈接,div的class=”d_1″下的所有l(wèi)i內(nèi)容。
一、HtmlCleaner使用:
1、HtmlCleaner
HtmlCleaner是一個(gè)開源的Java語(yǔ)言的Html文檔解析器。HtmlCleaner能夠重新整理HTML文檔的每個(gè)元素并生成結(jié)構(gòu)良好(Well-Formed)的 HTML 文檔。默認(rèn)它遵循的規(guī)則是類似于大部份web瀏覽器為創(chuàng)文檔對(duì)象模型所使用的規(guī)則。然而,用戶可以提供自定義tag和規(guī)則組來進(jìn)行過濾和匹配。
主頁(yè)地址:http://htmlcleaner.sourceforge.net/
下載地址://www.survivalescaperooms.com/softs/364983.html
2、基本示例,在wikipedia中抓取機(jī)場(chǎng)信息
html-clean-demo.html
html-clean-demo.html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "><html xmlns = "http://www.w3.org/1999/xhtml " xml:lang = "zh-CN" dir = "ltr"><head> <meta http-equiv = "Content-Type" content = "text/html; charset=GBK" /> <meta http-equiv = "Content-Language" content = "zh-CN" /> <title>html clean demo </title></head><body><div class = "d_1"> <ul> <li>bar </li> <li>foo </li> <li>gzz </li> </ul></div><div> <ul> <li><a name = "my_href" href = "1.html">text-1 </a></li> <li><a name = "my_href" href = "2.html">text-2 </a></li> <li><a name = "my_href" href = "3.html">text-3 </a></li> <li><a name = "my_href" href = "4.html">text-4 </a></li> </ul></div></body></html>
HtmlCleanerDemo.java
package com.chenlb;import java.io.File;import org.htmlcleaner.HtmlCleaner;import org.htmlcleaner.TagNode;/** * htmlcleaner 使用示例. * */public class HtmlCleanerDemo { public static void main(String[] args) throws Exception { HtmlCleaner cleaner = new HtmlCleaner(); TagNode node = cleaner.clean(new File("html/html-clean-demo.html"), "GBK"); //按tag取. Object[] ns = node.getElementsByName("title", true); //標(biāo)題 if(ns.length > 0) { System.out.println("title="+((TagNode)ns[0]).getText()); } System.out.println("ul/li:"); //按xpath取 ns = node.evaluateXPath("http://div[@class='d_1']//li"); for(Object on : ns) { TagNode n = (TagNode) on; System.out.println("/ttext="+n.getText()); } System.out.println("a:"); //按屬性值取 ns = node.getElementsByAttValue("name", "my_href", true, true); for(Object on : ns) { TagNode n = (TagNode) on; System.out.println("/thref="+n.getAttributeByName("href")+", text="+n.getText()); } }}
cleaner.clean()中的參數(shù),可以是文件,可以是url,可以是字符串內(nèi)容。比較常用的應(yīng)該是evaluateXPath、 getElementsByAttValue、getElementsByName方法了。另外說明下,htmlcleaner 對(duì)不規(guī)范的html兼容性比較好。
在wikipedia中抓取機(jī)場(chǎng)信息
import java.io.UnsupportedEncodingException;import org.htmlcleaner.HtmlCleaner;import org.htmlcleaner.TagNode;import org.htmlcleaner.XPatherException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;//import com.moore.index.BabyStory;import com.moore.util.HttpClientUtil;/** * 用途:TODO * * @author bbdtek */public class ParserAirport { private static Logger log = LoggerFactory.getLogger(ParserAirport.class); /** * @param args * @throws UnsupportedEncodingException * @throws XPatherException */ public static void main(String[] args) throws UnsupportedEncodingException, XPatherException { String url = "http://zh.wikipedia.org/wiki/%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD%E6%9C%BA%E5%9C%BA%E5%88%97%E8%A1%A8"; String contents = HttpClientUtil.getUtil().getCon(url); HtmlCleaner hc = new HtmlCleaner(); TagNode tn = hc.clean(contents); String xpath = "http://div[@class='mw-content-ltr']//table[@class='wikitable + sortable']//tbody//tr[@align='right']"; Object[] objarr = null; objarr = tn.evaluateXPath(xpath); if (objarr != null && objarr.length > 0) { for (Object obj : objarr) { TagNode tntr = (TagNode) obj; String xptr = "http://td[@align='left']//a"; Object[] objarrtr = null; objarrtr = tntr.evaluateXPath(xptr); if (objarrtr != null && objarrtr.length > 0) { for (Object obja : objarrtr) { TagNode tna = (TagNode) obja; String str = tna.getText().toString(); log.info(str); } } } } }}
二、XPath初探
1、XPath簡(jiǎn)介:
XPath 是一門在 XML 文檔中查找信息的語(yǔ)言。XPath 可用來在 XML 文檔中對(duì)元素和屬性進(jìn)行遍歷。
2、XPath節(jié)點(diǎn)選取
XPath 使用路徑表達(dá)式在 XML 文檔中選取節(jié)點(diǎn)。節(jié)點(diǎn)是通過沿著路徑或者 step 來選取的。
下面列出了最有用的路徑表達(dá)式:
表達(dá)式 | 描述 |
---|---|
nodename | 選取此節(jié)點(diǎn)的所有子節(jié)點(diǎn)。 |
/ | 從根節(jié)點(diǎn)選取。 |
// | 從匹配選擇的當(dāng)前節(jié)點(diǎn)選擇文檔中的節(jié)點(diǎn),而不考慮它們的位置。 |
. | 選取當(dāng)前節(jié)點(diǎn)。 |
.. | 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)。 |
@ | 選取屬性。 |
一些常用表達(dá)式
路徑表達(dá)式 | 結(jié)果 |
---|---|
/bookstore/book[1] | 選取屬于 bookstore 子元素的第一個(gè) book 元素。 |
/bookstore/book[last()] | 選取屬于 bookstore 子元素的最后一個(gè) book 元素。 |
/bookstore/book[last()-1] | 選取屬于 bookstore 子元素的倒數(shù)第二個(gè) book 元素。 |
/bookstore/book[position()<3] | 選取最前面的兩個(gè)屬于 bookstore 元素的子元素的 book 元素。 |
//title[@lang] | 選取所有擁有名為 lang 的屬性的 title 元素。 |
//title[@lang='eng'] | 選取所有 title 元素,且這些元素?fù)碛兄禐?eng 的 lang 屬性。 |
/bookstore/book[price>35.00] | 選取 bookstore 元素的所有 book 元素,且其中的 price 元素的值須大于 35.00。 |
/bookstore/book[price>35.00]/title | 選取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值須大于 35.00。 |
新聞熱點(diǎn)
疑難解答
圖片精選