【发布时间】:2015-08-10 04:11:47
【问题描述】:
我无法获取标签下的所有 html 代码。这是我当前的代码:
Document document = Jsoup.connect("http://stackoverflow.com/questions/2971155/what-is-the-fastest-way-to-scrape-html-webpage-in-android").get();
Elements desc = document.select("tr");
System.out.println(desc.toString());
这是针对那个问题的,我正在尝试从问题的描述中获取文本。但是我没有得到某些 tr 或 td 标签,例如问题中的标签。这是我想要获取的 td 标签:
<td class="postcell">
在该标签下是实际的帖子。现在,当我打印出我实际得到的内容时,我得到了大量空的 td 标签和一些 cmets,但不是实际的帖子。
<tr id="comment-37956942" class="comment ">
<td>
<table>
<tbody>
<tr>
<td class=" comment-score"> </td>
<td> </td>
</tr>
</tbody>
</table> </td>
<td class="comment-text">
<div style="display: block;" class="comment-body">
<span class="comcopy">You shouldn't parse HTML with regexes: <a href="http://blog.codinghorror.com/parsing-html-the-cthulhu-way/" rel="nofollow">blog.codinghorror.com/parsing-html-the-cthulhu-way</a></span> –
﹕ <a href="/users/25612/motob%c3%b3i" title="469 reputation" class="comment-user">motobói</a>
它继续使用空的 td 和 tr 标签。我找不到真正的问题。有人知道为什么会这样吗?
基本上,我只想要问题帖子中的文本,我不知道如何获取它,所以如果有人能告诉我如何获取文本会很好。
【问题讨论】:
-
听起来像是动态加载的。如果是这种情况,您将无法使用 jsoup 获取内容。除此之外,
get()方法返回一个Document,所以你不需要创建另一个Document。 -
是的,我用文档内容修复了代码,我只是在测试内容。不过,动态加载是什么意思?我仍然在 html 中得到一些 cmets。我只想知道如何从链接中的问题中获取文本。
-
试试这个选择器
td.postcell(就像你说的那样),如果你什么也没得到是因为帖子是动态加载的(例如ajax调用)。 -
是的,我之前尝试过,它什么也没给我。 “它正在动态加载”是什么意思?这是否意味着我无法获得职位?因为如果我使用“div”选择器,那么帖子就会显示出来(连同其他所有内容,因此很难将其过滤到帖子中)。