Jsoup开始解析AFTER指定的标签或从页面底部开始?

时间:2012-01-05 12:53:48

标签: java android html html-parsing jsoup

我有一个用Jsoup解析的HTML块,然而,并非所有这些都是相关的,并且解析不相关的部分会抛弃我的数据集。

在网站上,有一个标题可以随时更改。在这个标题内是链接,但我不关心的链接。当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();

2 个答案:

答案 0 :(得分: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.
    }

答案 1 :(得分:0)

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

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

<!-- end ad tag -->是我想忽略的开头,<!-- BEGIN TOPICS -->就是结束。