xpath find specific link in page

2019-07-24 12:53发布

I'm trying to get the email to a friend link from this page using xpath.

http://www.guardian.co.uk/education/2009/oct/14/30000-miss-university-place

The link itself is wrapped up in tags like this

            <li><a class="rollover sendlink" href="http://www.guardian.co.uk/email/354237257"  title="Opens an email form" name="&lid={pageToolbox}{Email a friend}&lpos={pageToolbox}{2}"><img src="http://static.guim.co.uk/static/80163/common/images/icon_email-friend.gif" alt="" class="trail-icon" /><span>Send to a friend</span></a></li>

I'm using this for my query, but it's not quite right.

            $links = $xpath->query("//a/span[text()='Send to a friend']/@href");

4条回答
Evening l夕情丶
2楼-- · 2019-07-24 13:08

You're trying to get the href of the span there. I think you want

$links = $xpath->query("//a[span/text()='Send to a friend']/@href");
查看更多
叼着烟拽天下
3楼-- · 2019-07-24 13:13

The href is an attribute of the anchor hence you need:-

 $links = $xpath->query("//a[span[text()='Send to a friend']]/@href");
查看更多
一纸荒年 Trace。
4楼-- · 2019-07-24 13:18

You need to use something like this (since href is an attribute of a):

$links = $xpath->query("//a[span/text()='Send to a friend']/@href");
查看更多
一夜七次
5楼-- · 2019-07-24 13:24

try this

 $links = $xpath->query("//a[span='Send to a friend']/@href");
查看更多
登录 后发表回答