Java 文本节点xpath的Jsoup CSS选择器

Java 文本节点xpath的Jsoup CSS选择器,java,css-selectors,jsoup,Java,Css Selectors,Jsoup,HTML代码发布在最后,我想选择“OF”元素。 这是CSS选择器 Elements position = doc.select("#content > table:nth-child(4) > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(1) > td > div:nth-child(5) > strong:nth-child(4)"); for (Eleme

HTML代码发布在最后,我想选择“OF”元素。 这是CSS选择器

Elements position = doc.select("#content > table:nth-child(4) > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(1) > td > div:nth-child(5) > strong:nth-child(4)");

for (Element p : position) {
    System.out.println(p);
}
这是输出

p returns "<strong>Position:</strong>"
p.text() returns "Position:"
HTML代码

<div style="font-size: 10pt; padding-left:5px;">
     <strong>Birthdate:</strong> 8/7/1991 (23 y, 6 m, 10 d) &nbsp;&nbsp;&nbsp;&nbsp;
     <strong>Bats/Throws:</strong> R/R &nbsp;&nbsp;&nbsp;&nbsp;
     <strong>Height/Weight:</strong> 6-2/230 &nbsp;&nbsp;&nbsp;&nbsp;
     <strong>Position:</strong> OF<br /><b>Drafted:</b> <a href="statss.aspx?playerid=10155&position=OF#draft" style="text-decoration:none;">2009 June Amateur Draft - Round: 1, Pick: 25, Overall: 25, Team: Los Angeles Angels</a><br />
     <strong>Contract:</strong> <a href="statss.aspx?playerid=10155&position=OF#contract" style="text-decoration:none;">$144.5M / 6 Years (2015 - 2020)</a>
</div>
<div style="font-size: 10pt; padding-left:5px;">
      <strong>Birthdate:</strong> 8/7/1991 (23 y, 6 m, 10 d) &nbsp;&nbsp;&nbsp;&nbsp;
      <strong>Bats/Throws:</strong> R/R &nbsp;&nbsp;&nbsp;&nbsp;
      <strong>Height/Weight:</strong> 6-2/230 &nbsp;&nbsp;&nbsp;&nbsp;
      <strong>Position:</strong> OF<br /><b>Drafted:</b> <a href="statss.aspx?playerid=10155&position=OF#draft" style="text-decoration:none;">2009 June Amateur Draft - Round: 1, Pick: 25, Overall: 25, Team: Los Angeles Angels</a><br />
      <strong>Contract:</strong> <a href="statss.aspx?playerid=10155&position=OF#contract" style="text-decoration:none;">$144.5M / 6 Years (2015 - 2020)</a>
</div>

出生日期:1991年8月7日(23岁,6米,10天)
击球/投掷:R/R
身高/体重:6-2/230
职位:起草人:
合同: 出生日期:1991年8月7日(23岁,6米,10天) 击球/投掷:R/R 身高/体重:6-2/230 职位:起草人:
合同:
如果有人感兴趣,请看这一页

无法为文本节点编写css选择器(“OF”是目标div元素中包含的第四个文本节点)。 因此,您需要像这样以编程方式获取(需要jsoup>=1.6.2):


“OF”是div元素内的第四个文本节点。不幸的是,在css中,您无法使用选择器将文本节点作为目标。我有jsoup 1.8.1,我得到以下错误,“类型元素的方法textNodes未定义”是的,对不起,我是在暗示。。。您必须从
文档中获取的
元素
集合中提取
元素
。选择
(例如,在示例中使用循环)感谢您的澄清,它现在可以工作了。谢谢你的帮助!
<div style="font-size: 10pt; padding-left:5px;">
     <strong>Birthdate:</strong> 8/7/1991 (23 y, 6 m, 10 d) &nbsp;&nbsp;&nbsp;&nbsp;
     <strong>Bats/Throws:</strong> R/R &nbsp;&nbsp;&nbsp;&nbsp;
     <strong>Height/Weight:</strong> 6-2/230 &nbsp;&nbsp;&nbsp;&nbsp;
     <strong>Position:</strong> OF<br /><b>Drafted:</b> <a href="statss.aspx?playerid=10155&position=OF#draft" style="text-decoration:none;">2009 June Amateur Draft - Round: 1, Pick: 25, Overall: 25, Team: Los Angeles Angels</a><br />
     <strong>Contract:</strong> <a href="statss.aspx?playerid=10155&position=OF#contract" style="text-decoration:none;">$144.5M / 6 Years (2015 - 2020)</a>
</div>
<div style="font-size: 10pt; padding-left:5px;">
      <strong>Birthdate:</strong> 8/7/1991 (23 y, 6 m, 10 d) &nbsp;&nbsp;&nbsp;&nbsp;
      <strong>Bats/Throws:</strong> R/R &nbsp;&nbsp;&nbsp;&nbsp;
      <strong>Height/Weight:</strong> 6-2/230 &nbsp;&nbsp;&nbsp;&nbsp;
      <strong>Position:</strong> OF<br /><b>Drafted:</b> <a href="statss.aspx?playerid=10155&position=OF#draft" style="text-decoration:none;">2009 June Amateur Draft - Round: 1, Pick: 25, Overall: 25, Team: Los Angeles Angels</a><br />
      <strong>Contract:</strong> <a href="statss.aspx?playerid=10155&position=OF#contract" style="text-decoration:none;">$144.5M / 6 Years (2015 - 2020)</a>
</div>
// select container div element
Elements position = doc.select("#content > table:nth-child(4) > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(1) > td > div:nth-child(5)");
// extract the element from the list returned
Element element = ....
// TODO will need to check that the List exists and have at least four elements here
TextNode ofNode = element.textNodes().get(4);
ofNode.text(); // this will contain "OF"