利用htmlparser提取网页纯文本的例子

import org.htmlparser.Node;

import org.htmlparser.NodeFilter;

import org.htmlparser.Parser;

importorg.htmlparser.filters.TagNameFilter;

import org.htmlparser.tags.TableTag;

import org.htmlparser.util.NodeList;

 

/**

 * 标题:利用htmlparser提取网页纯文本的例子

 */

public class TestHTMLParser2 {

       /**

        * 读取目标html内容

        *

        */

       publicstatic void testHtml() {

              try{

                     StringsCurrentLine;

                     StringsTotalString;

                     sCurrentLine= "";

                     sTotalString= "";

                     java.io.InputStreaml_urlStream;

                     java.net.URLl_url = new java.net.URL(

                                   "http://10.249.187.199:8083/injs100/");

                     java.net.HttpURLConnectionl_connection = (java.net.HttpURLConnection) l_url

                                   .openConnection();

                     l_connection.connect();

                     l_urlStream= l_connection.getInputStream();

                     java.io.BufferedReaderl_reader = new java.io.BufferedReader(

                                   newjava.io.InputStreamReader(l_urlStream));

                     while((sCurrentLine = l_reader.readLine()) != null) {

                            sTotalString+= sCurrentLine + "\r\n";

                     }

 

                     StringtestText = extractText(sTotalString);

              }catch (Exception e) {

                     e.printStackTrace();

              }

 

       }

   /**

    * 抽取纯文本信息

    * @param inputHtml:html文本

    * @return

    * @throws Exception

    */

       publicstatic String extractText(String inputHtml) throws Exception {

              StringBuffertext = new StringBuffer();

              Parserparser = Parser.createParser(new String(inputHtml.getBytes(),

                            "GBK"),"GBK");

              //遍历所有的节点

              NodeListnodes = parser.extractAllNodesThatMatch(new NodeFilter() {

                     publicboolean accept(Node node) {

                            returntrue;

                     }

              });

 

              System.out.println(nodes.size());

              for(int i = 0; i < nodes.size(); i++) {

                     Nodenodet = nodes.elementAt(i);

                     //字符串的代表性节点:节点的描述

                     text.append(newString(nodet.toPlainTextString().getBytes("GBK"))

                                   +"\r\n");

              }

              returntext.toString();

       }

   /**

    *  读取文件的方式/utl 来分析内容.filePath也可以是一个Url.

    * @param resource :文件/Url

    * @throws Exception

    */

       publicstatic void test5(String resource) throws Exception {

              ParsermyParser = new Parser(resource);

              myParser.setEncoding("GBK");

              StringfilterStr = "table";

              NodeFilterfilter = new TagNameFilter(filterStr);

              NodeListnodeList = myParser.extractAllNodesThatMatch(filter);

              /*for(inti=0;i<nodeList.size();i++)

              {

                     TableTagtabletag = (TableTag) nodeList.elementAt(i);

                     //标签名称

                     System.out.println(tabletag.getTagName());

                     System.out.println(tabletag.getText());

              }*/

              TableTagtabletag = (TableTag) nodeList.elementAt(1);

             

1.1    搜索引擎优化链接

http://c.tieba.baidu.com/p/3335169588
http://c.tieba.baidu.com/p/3335196448
http://c.tieba.baidu.com/p/3335205360
http://c.tieba.baidu.com/p/3335205360
http://c.tieba.baidu.com/p/3335206909
http://c.tieba.baidu.com/p/3335229010
http://c.tieba.baidu.com/p/3335231657
http://c.tieba.baidu.com/p/3335234359
http://c.tieba.baidu.com/p/3335234496
http://c.tieba.baidu.com/p/3335237439
http://c.tieba.baidu.com/p/3335240909
http://c.tieba.baidu.com/p/3335241858
http://c.tieba.baidu.com/p/3335243623
http://c.tieba.baidu.com/p/3335352252
http://c.tieba.baidu.com/p/3335363753
http://c.tieba.baidu.com/p/3333470019
http://c.tieba.baidu.com/p/3333434338
http://c.tieba.baidu.com/p/3333432664
http://c.tieba.baidu.com/p/3333431194
http://c.tieba.baidu.com/p/3333933445
http://c.tieba.baidu.com/p/3333921536
http://c.tieba.baidu.com/p/3333466836
http://c.tieba.baidu.com/p/3333426721
http://c.tieba.baidu.com/p/3333425017
http://c.tieba.baidu.com/p/3333417695
http://c.tieba.baidu.com/p/3333411577
http://c.tieba.baidu.com/p/3333411577
http://c.tieba.baidu.com/p/3334187475
http://c.tieba.baidu.com/p/3334188081
http://c.tieba.baidu.com/p/3334188751
http://c.tieba.baidu.com/p/3334198020
http://c.tieba.baidu.com/p/3334200716
http://c.tieba.baidu.com/p/3334201719
http://c.tieba.baidu.com/p/3334203677
http://c.tieba.baidu.com/p/3334205697
http://c.tieba.baidu.com/p/3334206661
http://c.tieba.baidu.com/p/3334208049
http://c.tieba.baidu.com/p/3334210061
http://c.tieba.baidu.com/p/3334211826
http://c.tieba.baidu.com/p/3334213032
http://c.tieba.baidu.com/p/3334214642
http://c.tieba.baidu.com/p/3334215369
http://c.tieba.baidu.com/p/3334216536
http://c.tieba.baidu.com/p/3334217502
http://c.tieba.baidu.com/p/3334218178
http://c.tieba.baidu.com/p/3334219429
http://c.tieba.baidu.com/p/3334221024
http://c.tieba.baidu.com/p/3334221665
http://c.tieba.baidu.com/p/3334249025
http://c.tieba.baidu.com/p/3334250596
http://c.tieba.baidu.com/p/3334254743
http://c.tieba.baidu.com/p/3334266933
http://c.tieba.baidu.com/p/3334280305
http://c.tieba.baidu.com/p/3334291575
http://c.tieba.baidu.com/p/3334294445
http://c.tieba.baidu.com/p/3334309801
http://c.tieba.baidu.com/p/3334209152
http://c.tieba.baidu.com/p/3334334707
http://c.tieba.baidu.com/p/3334334847
http://c.tieba.baidu.com/p/3334334992
http://c.tieba.baidu.com/p/3334337778
http://c.tieba.baidu.com/p/3334344163
http://c.tieba.baidu.com/p/3334344404
http://c.tieba.baidu.com/p/3334344668
http://c.tieba.baidu.com/p/3334345486
http://c.tieba.baidu.com/p/3333894788
http://c.tieba.baidu.com/p/3334005194
http://c.tieba.baidu.com/p/3334045317
http://c.tieba.baidu.com/p/3334053240
http://c.tieba.baidu.com/p/3334064936
http://c.tieba.baidu.com/p/3334070507
http://c.tieba.baidu.com/p/3333476131
http://c.tieba.baidu.com/p/3333472046
http://c.tieba.baidu.com/p/3333938049
http://c.tieba.baidu.com/p/3333935777
http://c.tieba.baidu.com/p/3333931648
http://c.tieba.baidu.com/p/3333924471
http://c.tieba.baidu.com/p/3333931648
http://c.tieba.baidu.com/p/3333924471
http://c.tieba.baidu.com/p/3333922991
http://c.tieba.baidu.com/p/3335365950
http://c.tieba.baidu.com/p/3335379539
http://c.tieba.baidu.com/p/3335422388
http://c.tieba.baidu.com/p/3335358701
http://c.tieba.baidu.com/p/3335416748
http://c.tieba.baidu.com/p/3335404382
http://c.tieba.baidu.com/p/3335564379
http://c.tieba.baidu.com/p/3335584345
http://c.tieba.baidu.com/p/3335615381
http://c.tieba.baidu.com/p/3335621543
http://c.tieba.baidu.com/p/3335633768
http://c.tieba.baidu.com/p/3335641133
http://c.tieba.baidu.com/p/3335656137
http://c.tieba.baidu.com/p/3335663594
http://c.tieba.baidu.com/p/3335695650
http://c.tieba.baidu.com/p/3338333881
http://c.tieba.baidu.com/p/3338329991
http://c.tieba.baidu.com/p/3338332074
http://c.tieba.baidu.com/p/3338334849
http://c.tieba.baidu.com/p/3338337453
http://c.tieba.baidu.com/p/3338339671
http://c.tieba.baidu.com/p/3338342433
http://c.tieba.baidu.com/p/3338351439
http://c.tieba.baidu.com/p/3338351918
http://c.tieba.baidu.com/p/3338355213
http://c.tieba.baidu.com/p/3338356761
http://c.tieba.baidu.com/p/3338358738
http://c.tieba.baidu.com/p/3338359848
http://c.tieba.baidu.com/p/3338633468
http://c.tieba.baidu.com/p/3338389937
http://c.tieba.baidu.com/f?kw=%C0%D6%B3%AF%D3%E9%C0%D6
http://c.tieba.baidu.com/f?kw=%C0%D6%B3%AF%D3%E9%C0%D6
http://c.tieba.baidu.com/p/3339269528
http://c.tieba.baidu.com/p/3339270876
http://c.tieba.baidu.com/p/3339279262
http://c.tieba.baidu.com/p/3339280772
http://c.tieba.baidu.com/p/3339283082
http://c.tieba.baidu.com/p/3339284983
http://c.tieba.baidu.com/p/3339286305
http://c.tieba.baidu.com/p/3339292858
http://c.tieba.baidu.com/p/3339294670
http://c.tieba.baidu.com/p/3339295849
http://c.tieba.baidu.com/p/3339297610
http://c.tieba.baidu.com/p/3339299302
http://c.tieba.baidu.com/p/3339300454
http://c.tieba.baidu.com/p/3339307995
http://c.tieba.baidu.com/p/3339309822
http://c.tieba.baidu.com/p/3322730487
http://c.tieba.baidu.com/p/3339223026
http://c.tieba.baidu.com/p/3339227372
http://c.tieba.baidu.com/p/3339228556
http://c.tieba.baidu.com/p/3339230431
http://c.tieba.baidu.com/p/3339232082
http://c.tieba.baidu.com/p/3339233684
http://c.tieba.baidu.com/p/3339235409
http://c.tieba.baidu.com/p/3339237031
http://c.tieba.baidu.com/p/3339255368
http://c.tieba.baidu.com/p/3339205434
http://c.tieba.baidu.com/p/3339206473
http://c.tieba.baidu.com/p/3339209087
http://c.tieba.baidu.com/p/3339210361
http://c.tieba.baidu.com/p/3339211740
http://c.tieba.baidu.com/p/3339213595
http://c.tieba.baidu.com/p/3339215171
http://c.tieba.baidu.com/p/3339216625
http://c.tieba.baidu.com/p/3339217904
http://c.tieba.baidu.com/p/3339189542
http://c.tieba.baidu.com/p/3339192267
http://c.tieba.baidu.com/p/3339194992
http://c.tieba.baidu.com/p/3339197756
http://c.tieba.baidu.com/p/3339199531
http://c.tieba.baidu.com/p/3339035178
http://c.tieba.baidu.com/p/3339169754
http://c.tieba.baidu.com/p/3339173392
http://c.tieba.baidu.com/p/3339174960
http://c.tieba.baidu.com/p/3339176874
http://c.tieba.baidu.com/p/3339179801
http://c.tieba.baidu.com/p/3339181677
http://c.tieba.baidu.com/p/3339004075
http://c.tieba.baidu.com/p/3339001347
http://c.tieba.baidu.com/p/3339006222
http://c.tieba.baidu.com/p/3339010770
http://c.tieba.baidu.com/p/3338993002
http://c.tieba.baidu.com/p/3338877884
http://c.tieba.baidu.com/p/3338863229
http://c.tieba.baidu.com/p/3338860756
http://c.tieba.baidu.com/p/3338858350
http://c.tieba.baidu.com/p/3338856241
http://c.tieba.baidu.com/p/3338853019
http://c.tieba.baidu.com/p/3338850203
http://c.tieba.baidu.com/p/3338846874
http://c.tieba.baidu.com/p/3338843758
http://c.tieba.baidu.com/p/3338841444
http://c.tieba.baidu.com/p/3338838655
http://c.tieba.baidu.com/p/3338835890
http://c.tieba.baidu.com/p/3338833821
http://c.tieba.baidu.com/p/3338831338
http://c.tieba.baidu.com/p/3338827331
http://c.tieba.baidu.com/p/3338781318
http://c.tieba.baidu.com/p/3338786602
http://c.tieba.baidu.com/p/3338805358
http://c.tieba.baidu.com/p/3338810141
http://c.tieba.baidu.com/p/3338812382
http://c.tieba.baidu.com/p/3338814111
http://c.tieba.baidu.com/p/3338815316
http://c.tieba.baidu.com/p/3338816968
http://c.tieba.baidu.com/p/3338817988
http://c.tieba.baidu.com/p/3338343455
http://c.tieba.baidu.com/p/3338337898
http://c.tieba.baidu.com/p/3332560593
http://c.tieba.baidu.com/p/3336560871
http://c.tieba.baidu.com/p/3336560871
http://c.tieba.baidu.com/p/3336570553
http://c.tieba.baidu.com/p/3336336572
http://c.tieba.baidu.com/p/3336337039
http://c.tieba.baidu.com/p/3336337780
http://c.tieba.baidu.com/p/3336325190
http://c.tieba.baidu.com/p/3336326493
http://c.tieba.baidu.com/p/3336326090
http://c.tieba.baidu.com/p/3336324288
http://c.tieba.baidu.com/p/3336325639
http://c.tieba.baidu.com/p/3336323710
http://c.tieba.baidu.com/p/3336320359
http://c.tieba.baidu.com/p/3336321539
http://c.tieba.baidu.com/p/3336322056
http://c.tieba.baidu.com/p/3336320837
http://c.tieba.baidu.com/p/3336319698
http://c.tieba.baidu.com/p/3336316403
http://c.tieba.baidu.com/p/3336314097
http://c.tieba.baidu.com/p/3336315741
http://c.tieba.baidu.com/p/3336314727
http://c.tieba.baidu.com/p/3336308674
http://c.tieba.baidu.com/p/3336307430
http://c.tieba.baidu.com/p/3336307409
http://c.tieba.baidu.com/p/3336306578
http://c.tieba.baidu.com/p/3336305871
http://c.tieba.baidu.com/p/3336077733
http://c.tieba.baidu.com/p/3336072110
http://c.tieba.baidu.com/p/3336066525
http://c.tieba.baidu.com/p/3336062113
http://c.tieba.baidu.com/p/3336057521
http://c.tieba.baidu.com/p/3336053254
http://c.tieba.baidu.com/p/3336048758


http://c.tieba.baidu.com/p/3340106758
http://c.tieba.baidu.com/p/3340105873
http://c.tieba.baidu.com/p/3340105154
http://c.tieba.baidu.com/p/3340048362
http://c.tieba.baidu.com/p/3340075633
http://c.tieba.baidu.com/p/3340066250
http://c.tieba.baidu.com/p/3340051822
http://c.tieba.baidu.com/p/3339999162
http://c.tieba.baidu.com/p/3339992842
http://c.tieba.baidu.com/p/3340040005
http://c.tieba.baidu.com/p/3339996722
http://c.tieba.baidu.com/p/3340044696
http://c.tieba.baidu.com/p/3340005046
http://c.tieba.baidu.com/p/3340012133
http://c.tieba.baidu.com/p/3339990355
http://c.tieba.baidu.com/p/3339997893
http://c.tieba.baidu.com/p/3340075633
http://c.tieba.baidu.com/p/3340048362
http://c.tieba.baidu.com/p/3340066250
http://c.tieba.baidu.com/p/3340051822
http://c.tieba.baidu.com/p/3339999162
http://c.tieba.baidu.com/p/3339992842
http://c.tieba.baidu.com/p/3340040005
http://c.tieba.baidu.com/p/3340005046
http://c.tieba.baidu.com/p/3340010806
http://c.tieba.baidu.com/p/3339995501
http://c.tieba.baidu.com/p/3340044696
http://c.tieba.baidu.com/p/3339996722
http://c.tieba.baidu.com/p/3340006902
http://c.tieba.baidu.com/p/3340012133
http://c.tieba.baidu.com/p/3339997893
http://c.tieba.baidu.com/p/3340008466
http://c.tieba.baidu.com/p/3340014454
http://c.tieba.baidu.com/p/3340045990
http://c.tieba.baidu.com/p/3339991593
http://c.tieba.baidu.com/p/3340043547
http://c.tieba.baidu.com/p/3340001805
http://c.tieba.baidu.com/p/3340009682
http://c.tieba.baidu.com/p/3339994027
http://c.tieba.baidu.com/p/3340042066
http://c.tieba.baidu.com/p/3340000308
http://c.tieba.baidu.com/p/3340019973
http://c.tieba.baidu.com/p/3340021053
http://c.tieba.baidu.com/p/3339986854
http://c.tieba.baidu.com/p/3339989009
http://c.tieba.baidu.com/p/3338337898

              

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。