2012-01-05 111 views
1

我有一块HTML,我用Jsoup解析,然而,并不是所有的都是相关的,解析不相关的部分抛出我的数据集。Jsoup开始解析AFTER指定的标签或从页面底部开始?

在该网站上,有一个标题可以随时更改。在这个头里面是链接,但是我不关心的链接。当Jsoup解析文档时,它会将这些文件添加到我的链接数组中,并抛出我的值。

我感兴趣的HTML之后是 <!-- BEGIN TOPICS --> 标记。

我希望能够告诉Jsoup忽略标签上方的所有内容。这可能吗?如果没有,我可以通过在文档底部开始解析来解决这个问题,但我不知道我会如何去解决这个问题。

我的Jsoup查询如下。请忽略所有的注释行和调试语句,我一直在努力工作,这一点了一会儿,仍然在测试代码。

 Thread getTitlesThread = new Thread() { 
      public void run() { 
       TitleResults titleArray = new TitleResults(); 
       StringBuilder whole = new StringBuilder(); 

       try { 
        URL url = new URL(
          Constants.FORUM); 
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); 
        try { 
         BufferedReader in = new BufferedReader(
          new InputStreamReader(new BufferedInputStream(urlConnection.getInputStream()))); 
         String inputLine; 
         while ((inputLine = in.readLine()) != null) 
          whole.append(inputLine); 
         in.close(); 
        } catch (IOException e) {} 
        finally { 
         urlConnection.disconnect(); 
        } 
       } catch (Exception e) {} 
       Document doc = Parser.parse(whole.toString(), Constants.FORUM); 
       Elements threads = doc.select("TOPICS > .topic_title"); 
       Elements authors = doc.select("a[hovercard-ref]"); 
//    for (Element author : authors) { 
//     authorArray.add(author.text()); 
//    } 
//    cleanAuthors(); 
       if (threads.isEmpty()) { 
        Log.d("POC", "EMPTY BRO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11"); 
       } 
//    for (Element thread : threads) { 
//     titleArray = new TitleResults(); 
//     Log.d("POC", thread.toString()); 
// 
//     titleArray.setAuthorDate(authorArray.get(0)); 
//     authorArray.remove(0); 

        //Thread title 
//     threadTitle = thread.text(); 
//     titleArray.setItemName(threadTitle); 
//     
//     //Thread link 
//     String threadStr = thread.attr("abs:href"); 
//     String endTag = "/page__view__getnewpost"; //trim link 
//     threadStr = new String(threadStr.replace(endTag, "")); 
//     threadArray.add(threadStr); 
//     results.add(titleArray); 
//    } 
      } 
     }; 
     getTitlesThread.start(); 
+0

我能得到它 '书籍DOC = Parser.parse(whole.toString()工作的replaceAll(“<! - 结束广告代码 - > * < - ?。?!BEGIN话题 - >“,”“),Constants.FORUM);' 其中'<! - end ad tag - >'是我想要忽略的开始,'<! - BEGIN TOPICS - > '结束了。 – r2DoesInc 2012-01-05 13:54:00

回答

0

删除您不想与解析文档的一部分:

Document doc = Parser.parse(whole.toString().replaceAll("<!-- end ad tag -->?.*?<!-- BEGIN TOPICS -->", ""), Constants.FORUM); 

<!-- end ad tag -->是世界卫生大会的开始我想忽略和<!-- BEGIN TOPICS -->是结束。

+0

这是什么Constants.FORUM?你能链接到适当的javadoc什么的? – Sparker0i 2017-05-11 18:20:04

1

这应该工作,因为你的描述(很难受某些不实际的HTML输入):

Document document = ...; 
    Elements elements = document.getAllElements(); 
    Element comment = null; 
    int size = elements.size(); 
    for (int i = 0; comment == null && i < size; i++) { 
     Element element = elements.get(i); 
     for (Node node : element.childNodes()) { 
      if (node instanceof Comment) { 
       String str = ((Comment) node).getData().trim(); 
       if ("BEGIN TOPICS".equals(str)) { 
        comment = element; 
        break; 
       } 
      } 
     } 
    } 

    // Did we find <-- BEGIN TOPICS -->? 
    if (comment != null) { 
     // You can now select from the siblingElements of comment 
     // and only get stuff "after" that comment: 
     // e.g. Elements e = comment.siblingElements().select("a"); 
    } else { 
     // Oh snap. 
    } 
+0

如果你检查我对这个问题所做的评论,我最终只是删除了我不想要的所有内容,然后才开始分析这些元素。不幸的是,我的代表不够高,不能回答我自己的问题7个小时。 – r2DoesInc 2012-01-05 14:28:29