Jsoup:select() 在不应该返回空的时候返回空

发布于 2024-12-08 19:31:38 字数 1383 浏览 3 评论 0原文

我正在尝试选择维基百科的 Google 条目页面上的信息框:http://en.m.wikipedia。 org/wiki/Google

所以,我调用:

contentDiv = document.select("div[id=content]").first();

哪个按预期工作,然后我这样做:

Elements infoboxes = contentDiv.select("table[class=infobox]");

然后我检查 infoboxes.isEmpty() ,我惊讶地发现它是空的!

我检查并验证了元素 contentDiv 包含以下内容:

<table class="infobox vcard" style="width: 22em;" cellspacing="5">

那么,为什么 contentDiv.select("table[class=infobox]") 返回空???

更新:我用 contentDiv.select("table[class=infobox vcard]") 测试了上面的内容,效果很好!这很奇怪,因为我知道与仅选择exact多类元素的 table.infobox.vcard 表示法不同,table[class=infobox]应选择在其列出的类中至少 infobox 的所有表。

顺便说一句,我使用另一个维基百科条目测试了代码,

<table class="infobox biota" style="text-align: left; width: 200px; font-size: 100%;">

其中 包含: >contentDiv.select("table[class=infobox]") 的行为与预期完全一致,返回该表元素作为 infoboxes 中的第一项。

知道为什么不一致吗?什么可以解释这种奇怪的行为?

有没有可能我只是偶然发现了 Jsoup bug?

(我使用的是 jsoup-1.5.2,不是最新的,但我不需要 HTML5 支持,并且由于各种原因我无法立即升级到最新的 1.6.1)。

I am trying to select the infobox on Wikipedia's Google entry page: http://en.m.wikipedia.org/wiki/Google

So, I call:

contentDiv = document.select("div[id=content]").first();

Which works as expected, then I do:

Elements infoboxes = contentDiv.select("table[class=infobox]");

Then I check infoboxes.isEmpty() and I am stunned to discover that it is empty!

I checked and verified that the element contentDiv contains the following:

<table class="infobox vcard" style="width: 22em;" cellspacing="5">

So, why does contentDiv.select("table[class=infobox]") return empty???

UPDATE: I tested the above with contentDiv.select("table[class=infobox vcard]") and it works fine! This is weird since I know that unlike the table.infobox.vcard notation which only selects the exact multiclass element, table[class=infobox] should select all tables that have at least infobox in their listed classes.

BTW, I tested the code, with a different Wikipedia entry, containing:

<table class="infobox biota" style="text-align: left; width: 200px; font-size: 100%;">

And that contentDiv.select("table[class=infobox]") behaves exactly as expected, returning that table element as the first item in infoboxes.

Any idea why the inconsistency? What could explain this odd behavior?

Is it possible that I just stumbled on a Jsoup bug?

(I'm using jsoup-1.5.2, not the latest but I don't need HTML5 support and for various reasons I can't upgrade immediately to the latest 1.6.1).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁许谁一生繁华 2024-12-15 19:31:38

[attributename=attributevalue] 选择器是一个 完全匹配。这是在 CSS 选择器规范 中指定的(重点是我的):

[att=val]
       当元素的“att”属性值为完全“val”时匹配。

您想要使用 [attributename~=attributevalue]相反:

Elements infoboxes = contentDiv.select("table[class~=infobox]");
// ...

或者,实际上更好的是 .classname 选择器:

Elements infoboxes = contentDiv.select("table.infobox");
// ...

另请参阅:


至于您使用不同维基百科条目的测试,我无法重现这一点。但我可以看出该页面包含另一个 它一定是您实际检索的那个。

The [attributename=attributevalue] selector is an exact match. This is specified in CSS selector spec (emphasis mine):

[att=val]
        Match when the element's "att" attribute value is exactly "val".

You want to use the [attributename~=attributevalue] instead:

Elements infoboxes = contentDiv.select("table[class~=infobox]");
// ...

or, better actually, the .classname selector:

Elements infoboxes = contentDiv.select("table.infobox");
// ...

See also:


As to your test with different Wikipedia entry, I can't reproduce this. But I can tell that this page contains another <table class="infobox"> which must be the one you're actually retrieving.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文