今天就跟大家聊聊有關(guān)Java如何實(shí)現(xiàn)基于模板的網(wǎng)頁(yè)結(jié)構(gòu)化信息精準(zhǔn)抽取組件HtmlExtractor,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)建站從2013年開(kāi)始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站制作、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元桑珠孜做網(wǎng)站,已為上家服務(wù),為桑珠孜各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
HtmlExtractor是一個(gè)Java實(shí)現(xiàn)的基于模板的網(wǎng)頁(yè)結(jié)構(gòu)化信息精準(zhǔn)抽取組件,本身并不包含爬蟲(chóng)功能,但可被爬蟲(chóng)或其他程序調(diào)用以便更精準(zhǔn)地對(duì)網(wǎng)頁(yè)結(jié)構(gòu)化信息進(jìn)行抽取。
HtmlExtractor由2個(gè)子項(xiàng)目構(gòu)成,html-extractor和html-extractor-web。 html-extractor實(shí)現(xiàn)了數(shù)據(jù)抽取邏輯,是從節(jié)點(diǎn),html-extractor-web提供web界面來(lái)維護(hù)抽取規(guī)則,是主節(jié)點(diǎn)。 html-extractor是一個(gè)jar包,可通過(guò)maven引用:
org.apdplat html-extractor 1.0
html-extractor-web是一個(gè)war包,需要部署到Servlet/Jsp容器上。
//1、構(gòu)造抽取規(guī)則 ListurlPatterns = new ArrayList<>(); //1.1、構(gòu)造URL模式 UrlPattern urlPattern = new UrlPattern(); urlPattern.setUrlPattern("http://money.163.com/\\d{2}/\\d{4}/\\d{2}/[0-9A-Z]{16}.html"); //1.2、構(gòu)造HTML模板 HtmlTemplate htmlTemplate = new HtmlTemplate(); htmlTemplate.setTemplateName("網(wǎng)易財(cái)經(jīng)頻道"); htmlTemplate.setTableName("finance"); //1.3、將URL模式和HTML模板建立關(guān)聯(lián) urlPattern.addHtmlTemplate(htmlTemplate); //1.4、構(gòu)造CSS路徑 CssPath cssPath = new CssPath(); cssPath.setCssPath("h2"); cssPath.setFieldName("title"); cssPath.setFieldDescription("標(biāo)題"); //1.5、將CSS路徑和模板建立關(guān)聯(lián) htmlTemplate.addCssPath(cssPath); //1.6、構(gòu)造CSS路徑 cssPath = new CssPath(); cssPath.setCssPath("div#endText"); cssPath.setFieldName("content"); cssPath.setFieldDescription("正文"); //1.7、將CSS路徑和模板建立關(guān)聯(lián) htmlTemplate.addCssPath(cssPath); //可象上面那樣構(gòu)造多個(gè)URLURL模式 urlPatterns.add(urlPattern); //2、獲取抽取規(guī)則對(duì)象 ExtractRegular extractRegular = ExtractRegular.getInstance(urlPatterns); //注意:可通過(guò)如下3個(gè)方法動(dòng)態(tài)地改變抽取規(guī)則 //extractRegular.addUrlPatterns(urlPatterns); //extractRegular.addUrlPattern(urlPattern); //extractRegular.removeUrlPattern(urlPattern.getUrlPattern()); //3、獲取HTML抽取工具 HtmlExtractor htmlExtractor = HtmlExtractor.getInstance(extractRegular); //4、抽取網(wǎng)頁(yè) String url = "http://money.163.com/08/1219/16/4THR2TMP002533QK.html"; List extractResults = htmlExtractor.extract(url, "gb2312"); //5、輸出結(jié)果 int i = 1; for (ExtractResult extractResult : extractResults) { System.out.println((i++) + "、網(wǎng)頁(yè) " + extractResult.getUrl() + " 的抽取結(jié)果"); for(ExtractResultItem extractResultItem : extractResult.getExtractResultItems()){ System.out.print("\t"+extractResultItem.getField()+" = "+extractResultItem.getValue()); } System.out.println("\tdescription = "+extractResult.getDescription()); System.out.println("\tkeywords = "+extractResult.getKeywords()); }
1、運(yùn)行主節(jié)點(diǎn),負(fù)責(zé)維護(hù)抽取規(guī)則: 將子項(xiàng)目html-extractor-web打成War包然后部署到Tomcat。 2、獲取一個(gè)HtmlExtractor的實(shí)例(從節(jié)點(diǎn)),示例代碼如下:
String allExtractRegularUrl = "http://localhost:8080/HtmlExtractorServer/api/all_extract_regular.jsp"; String redisHost = "localhost"; int redisPort = 6379; HtmlExtractor htmlExtractor = HtmlExtractor.getInstance(allExtractRegularUrl, redisHost, redisPort);
3、抽取信息,示例代碼如下:
String url = "http://money.163.com/08/1219/16/4THR2TMP002533QK.html"; ListextractResults = htmlExtractor.extract(url, "gb2312"); int i = 1; for (ExtractResult extractResult : extractResults) { System.out.println((i++) + "、網(wǎng)頁(yè) " + extractResult.getUrl() + " 的抽取結(jié)果"); for(ExtractResultItem extractResultItem : extractResult.getExtractResultItems()){ System.out.print("\t"+extractResultItem.getField()+" = "+extractResultItem.getValue()); } System.out.println("\tdescription = "+extractResult.getDescription()); System.out.println("\tkeywords = "+extractResult.getKeywords()); }
看完上述內(nèi)容,你們對(duì)Java如何實(shí)現(xiàn)基于模板的網(wǎng)頁(yè)結(jié)構(gòu)化信息精準(zhǔn)抽取組件HtmlExtractor有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。