【问题标题】:Jsoup begin parsing AFTER specified tag or start from bottom of page?Jsoup在指定标签之后开始解析还是从页面底部开始?
【发布时间】:2012-01-05 12:53:47
【问题描述】:

我有一个 HTML 块,我正在用 Jsoup 解析,但是,并不是所有的都是相关的,解析不相关的部分会抛出我的数据集。

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

【问题讨论】:

  • 我能够让它与Document doc = Parser.parse(whole.toString().replaceAll("<!-- end ad tag -->?.*?<!-- BEGIN TOPICS -->", ""), Constants.FORUM); 一起工作,其中<!-- end ad tag --> 是我想忽略的开始,<!-- BEGIN TOPICS --> 是结束。

标签: java android html html-parsing jsoup


【解决方案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.
    }

【讨论】:

  • 如果您查看我对该问题所做的评论,我最终只是在开始解析元素之前删除了我不想要的所有内容。不幸的是,我的代表还不够高,我无法再用 7 个小时回答我自己的问题。
【解决方案2】:

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

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

&lt;!-- end ad tag --&gt; 是我想忽略的开始,&lt;!-- BEGIN TOPICS --&gt; 是结束。

【讨论】:

  • 这个 Constants.FORUM 是什么?你能链接到适当的 javadoc 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2015-02-26
  • 2019-09-20
  • 2020-03-01
  • 2022-12-14
相关资源
最近更新 更多