代码之家  ›  专栏  ›  技术社区  ›  Tom

xpath在页面中查找特定链接

  •  1
  • Tom  · 技术社区  · 15 年前

    我正在尝试使用xpath从该页面获取指向朋友的电子邮件链接。

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

    链接本身被这样的标签包装起来

                <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>
    

    我用这个查询,但不太对。

                $links = $xpath->query("//a/span[text()='Send to a friend']/@href");
    
    4 回复  |  直到 15 年前
        1
  •  3
  •   philnash    15 年前

    你想在那里得到翼展的Href。我想你想要

    $links = $xpath->query("//a[span/text()='Send to a friend']/@href");
    
        2
  •  1
  •   rslite    15 年前

    你需要用这样的东西(因为 HREF 是的属性 ):

    $links = $xpath->query("//a[span/text()='Send to a friend']/@href");
    
        3
  •  1
  •   AnthonyWJones    15 年前

    Href是锚的一个属性,因此您需要:

     $links = $xpath->query("//a[span[text()='Send to a friend']]/@href");
    
        4
  •  1
  •   Andrew Keith    15 年前

    试试这个

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