Jsoup:select() 在不应该返回空的时候返回空
我正在尝试选择维基百科的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[attributename=attributevalue]
选择器是一个 完全匹配。这是在 CSS 选择器规范 中指定的(重点是我的):您想要使用
[attributename~=attributevalue]
相反:或者,实际上更好的是
.classname
选择器:另请参阅:
Selector
API至于您使用不同维基百科条目的测试,我无法重现这一点。但我可以看出该页面包含另一个
它一定是您实际检索的那个。
The
[attributename=attributevalue]
selector is an exact match. This is specified in CSS selector spec (emphasis mine):You want to use the
[attributename~=attributevalue]
instead:or, better actually, the
.classname
selector:See also:
Selector
APIAs 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.