这是获取此HTML字符串中唯一的MP3 href的正确xpath查询语句吗?

时间:2011-11-21 02:36:58

标签: html url xpath extract href

我在我的代码中使用的xpath查询遇到了一些问题,我想我会把它扔出去并询问社区这个查询是否实际上是为了预期目的而正确制定的​​。如下面的代码所示,我希望获取标签的href属性中唯一的MP3 URL。这整段HTML都作为字符串输入xpath。

xpath查询:

$hrefs = $xpath->query("//a[contains(@href,'mp3')]/@href");

将HTML输入查询:

<img class="myclass" title="mytitle" src="http://www.mydomain.com/myfolder/mypic.jpg" alt="myalt" width="552" height="414" />

[be-linked-title-info]

<a title="mytitle" href="https://s3.amazonaws.com/myfolder/published/RJD2+-+SEVEN+LIGHT+YEARS+(INSTRUMENTAL).mp3">Song Name and Artist</a>

The written plain text post entry describing this music track goes here and says blah blah blah

1 个答案:

答案 0 :(得分:3)

这可能适用于您的输入,但它会在mp3属性中的任何位置匹配href,这可能不是您想要的。更严格的方法只匹配以.mp3结尾的字符串。像这样:

//a['.mp3' = substring(@href, string-length(@href) - 
                              string-length('.mp3') + 1)]/@href
相关问题