用于解析“SRC”的 XPath 来自 IMG 标签?
现在,我成功地从 HTML 页面中获取了完整的元素:
//img[@class='photo-large']
例如,它将返回:
<img src="http://example.com/img.jpg" class='photo-large' />
但我只需要 SRC url (http://example.com/img.jpg)。 有什么帮助吗?
Right now I successfully grabbed the full element from an HTML page with this:
//img[@class='photo-large']
for example it would return this:
<img src="http://example.com/img.jpg" class='photo-large' />
But I only need the SRC url (http://example.com/img.jpg). Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
//img/@src
如果你想要图像的链接,你可以使用这个。
例子:
//img/@src
you can just go with this if you want a link of the image.
example:
使用 Hpricot 可以实现此功能:
如果您有多个图像,则以下给出一个数组:
但是,Nokogiri 是 快很多倍,而且 “可以用作 Hpricot 的替代品”。
这里是 Nokogiri 的版本,其中用于选择属性的 XPath 有效:
或者对于许多图像:
Using Hpricot this works:
In case you have more than one image, the following gives an array:
However, Nokogiri is many times faster and it “can be used as a drop in replacement” for Hpricot.
Here the version for Nokogiri, in which this XPath for selecting attributes works:
or for many images:
你自己已经很接近回答这个问题了,所以我有点不愿意为你回答。 然而,下面的 XPath 应该提供您想要的内容(当然,前提是源是 XHTML)。
如需更多提示,请查看 W3 学校。 他们有关于此类事情的优秀教程和很好的参考资料。
You are so close to answering this yourself that I am somewhat reluctant to answer it for you. However, the following XPath should provide what you want (provided the source is XHTML, of course).
For further tips, check out W3 Schools. They have excellent tutorials on such things and a great reference too.