次のサイトをスクレイプしようとしています:http://www.hudson211.org/zf/profile/service/id/659837
「ウェブアドレス」のテキストの横にあるhrefを選択しようとしています。次のxpathセレクターは、私が探しているタグを取得します。
$x("//th[contains(text(), 'Web Address')]/following-sibling::td/a")
戻り値
<a href="http://www.co.sullivan.ny.us">www.co.sullivan.ny.us</a>
ただし、@ hrefを使用してhrefを具体的に抽出しようとすると、戻り値は空の配列になります。
$x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href")
戻り値 []
これは私が見ている行の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>
その$x()
機能のためにGoogleChromeコンソールを使用していると思います。Chromeでテストしたように、@href
属性を選択するxpathは実際に機能しましたが、要素を選択したときのように結果のみがコンソールに表示されません-現時点ではよくわからないためです-:
>var result = $x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href")
undefined
>result[0].value
"http://www.co.sullivan.ny.us"
まったく同じ式を使用すると、変数result
に期待されるURL値が含まれていることがわかります。href
それ以上の処理を行わずにコンソールに単一の値を表示するだけの場合は、次のようにします。
>$x("//th[contains(text(), 'Web Address')]/following-sibling::td/a/@href")[0].value
"http://www.co.sullivan.ny.us"
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加