2016-03-25 135 views
0

我试图解析一个网站来提取人名和国家。如何获得以下兄弟::文本()和以下兄弟:: b?

页面有时看起来像:

<th>Inventors:</th> 
    <td align="left" width="90%"> 
      <b>Harvey; John Christopher</b> (New York, NY)<b>, Cuddihy; James William</b> (New York, NY) 
    </td> 

我能得到使用国家

//th[contains(text(), "Inventors:")]/following-sibling::td/b[contains(text(),";")]/following-sibling::text() 

[(New York, NY), (New York, NY)] 

有时页面看起来像(添加围绕国名):

<th>Inventors:</th> 
    <td align="left" width="90%"> 
     <b>Harvey; John Christopher</b> (New York, <b>NY</b>)<b>, Cuddihy; James William</b> (New York, <b>NY</b>) 
    </td> 

我可以得到国家:

//th[contains(text(), "Inventors:")]/following-sibling::td/b[contains(text(),";")]/following-sibling::b 

[NY, NY] 

现在,我希望能够在两种情况下获得国家。

我试着用:

//th[contains(text(), "Inventors:")]/following-sibling::td/b[contains(text(),";")]/following-sibling::*[self::text() or self::b] 

但当时我只得到 “B” S ...

我也试过:

//.../following-sibling::text() | //.../following-sibling::b 

但我也只得到“b”...

任何想法为什么这不按预期方式工作?任何解决方案来获得这两个条目

回答

1

您可以使用

string(//th[.="Inventors:")]/following-sibling::td) 

所以,你会选择

Harvey; John Christopher (New York, NY), Cuddihy; James William (New York, NY) 
在这两种情况下

。然后使用XPath 2.0字符串/正则表达式处理函数,或者如果只有XPath 1.0可用,则使用调用语言中的这些工具。

0

你也可以尝试类似:

//th[contains(text(), "Inventors:")] 
    /following-sibling::td/b[contains(text(),";")] 
    /following-sibling::node()[not(self::b[contains(text(),";")])] 

这将选择以下所有同胞节点,但是忽略包含A B节点“;”。