htmlcleaner應用辦法及xpath語法初探。本站提示廣大學習愛好者:(htmlcleaner應用辦法及xpath語法初探)文章只能為提供參考,不一定能成為您想要的結果。以下是htmlcleaner應用辦法及xpath語法初探正文
在編程的時刻或許寫收集爬蟲的時刻,常常須要對html停止解析,抽取個中有效的數據。一款好的對象是特殊有效的,能供給許多的贊助,網上有許多如許的對象,好比:htmlcleaner、htmlparser
經應用比擬:感到 htmlcleaner 比 htmlparser 好用,特別是htmlcleaner 的 xpath特好用。
上面針對htmlcleaner停止舉例解釋,需求為:掏出title,name=”my_href” 的鏈接,div的class=”d_1″下的一切li內容。
1、HtmlCleaner應用:
1、HtmlCleaner
HtmlCleaner是一個開源的Java說話的Html文檔解析器。HtmlCleaner可以或許從新整頓HTML文檔的每一個元素並生成構造優越(Well-Formed)的 HTML 文檔。默許它遵守的規矩是相似於年夜部分web閱讀器為創文檔對象模子所應用的規矩。但是,用戶可以供給自界說tag和規矩組來停止過濾和婚配。
主頁地址:http://htmlcleaner.sourceforge.net/
下載地址:http://www.jb51.net/softs/364983.html
2、根本示例,在wikipedia中抓取機場信息
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); //題目 if(ns.length > 0) { System.out.println("title="+((TagNode)ns[0]).getText()); } System.out.println("ul/li:"); //按xpath取 ns = node.evaluateXPath("//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()中的參數,可所以文件,可所以url,可所以字符串內容。比擬經常使用的應當是evaluateXPath、 getElementsByAttValue、getElementsByName辦法了。別的解釋下,htmlcleaner 對不標准的html兼容性比擬好。
在wikipedia中抓取機場信息
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 = "//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 = "//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); } } } } } }
2、XPath初探
1、XPath簡介:
XPath 是一門在 XML 文檔中查找信息的說話。XPath 可用來在 XML 文檔中對元素和屬性停止遍歷。
2、XPath節點拔取
XPath 應用途徑表達式在 XML 文檔當選取節點。節點是經由過程沿著途徑或許 step 來拔取的。
上面列出了最有效的途徑表達式:
一些經常使用表達式