java使用htmlparser提取網頁純文字例子_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

package com.test;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;

/**
* 標題:利用htmlparser提取網頁純文字的例子
*/
public class TestHTMLParser {
  public static void testHtml() {
    try {
        String sCurrentLine;
        String sTotalString;
        sCurrentLine = "";
        sTotalString = "";
        java.io.InputStream l_urlStream;
        java.net.URL l_url = new java.net.URL("http://www.ideagrace.com/html/doc/2006/07/04/00929.html");
        java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
        l_connection.connect();
        l_urlStream = l_connection.getInputStream();
        java.io.BufferedReader l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(l_urlStream));
        while ((sCurrentLine = l_reader.readLine()) != null) {
          sTotalString += sCurrentLine+"/r/n";
        //  System.out.println(sTotalString);
        }
        String testText = extractText(sTotalString);
        System.out.println( testText );

    } catch (Exception e) {
        e.printStackTrace();
    }

  }

  public static String extractText(String inputHtml) throws Exception {
    StringBuffer text = new StringBuffer();
    Parser parser = Parser.createParser(new String(inputHtml.getBytes(),"GBK"), "GBK");
    // 遍曆所有的節點
    NodeList nodes = parser.extractAllNodesThatMatch(new NodeFilter() {
        public boolean accept(Node node) {
          return true;
        }
    });

    System.out.println(nodes.size()); //列印節點的數量
    for (int i=0;i<nodes.size();i++){
         Node nodet = nodes.elementAt(i);
         //System.out.println(nodet.getText());
        text.append(new String(nodet.toPlainTextString().getBytes("GBK"))+"/r/n");         
    }
    return text.toString();
  }

  public static void test5(String resource) throws Exception {
    Parser myParser = new Parser(resource);
    myParser.setEncoding("GBK");
    String filterStr = "table";
    NodeFilter filter = new TagNameFilter(filterStr);
    NodeList nodeList = myParser.extractAllNodesThatMatch(filter);
    TableTag tabletag = (TableTag) nodeList.elementAt(11);

  }

  public static void main(String[] args) throws Exception {
    // test5("http://www.google.com");
    testHtml();
  }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.