简体   繁体   English

使用XPath选择以下兄弟的href属性

[英]Using XPath to select the href attribute of the following-sibling

I am attempting to scrape the following site: http://www.hudson211.org/zf/profile/service/id/659837 我试图刮掉以下网站: http//www.hudson211.org/zf/profile/service/id/659837

I am trying to select the href next to the "web address" text. 我正在尝试选择“网址”文本旁边的href。 The following xpath selector gets the tag I am after: 以下xpath选择器获取我之后的标记:

$x("//th[contains(text(), 'Web Address')]/following-sibling::td/a")

returns 回报

<a href="http://www.co.sullivan.ny.us">www.co.sullivan.ny.us</a>

However, when I specifically try to extract the href using @href, the return value is an empty array: 但是,当我专门尝试使用@href提取href时,返回值是一个空数组:

$x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href")

returns [] 返回[]

This is the html of the row I am looking at: 这是我正在查看的行的html:

<tr valign="top">
    <td class="profile_view_left"></td>
    <th align="left" class="profile_view_center">Web Address</th>
    <td class="profile_view_right">
      <ahref="http://www.co.sullivan.ny.us">www.co.sullivan.ny.us</a>                         </td>
    <td></td>
</tr>

I assume you're using Google Chrome console because of that $x() function. 我假设你使用的是谷歌Chrome控制台,因为这个$x()函数。 Your xpath which selects @href attribute actually worked , as I tested in my Chrome, only the result is not displayed in the console like when you selected an element -for a reason that I'm not quite sure at the moment- : 选择@href属性的xpath 确实有效 ,正如我在Chrome中测试的那样,只有结果不会像控件中那样显示在控制台中 - 这是我目前不太确定的原因 - :

>var result = $x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href")
undefined
>result[0].value
"http://www.co.sullivan.ny.us"

see that using the exact same expression, variable result contains the expected url value. 看到使用完全相同的表达式,变量result包含预期的url值。 If your intention is simply to display single href value in the console without further processing, this will do : 如果您的目的只是在控制台中显示单个href值而无需进一步处理,则会执行以下操作:

>$x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href")[0].value
"http://www.co.sullivan.ny.us"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM