现在我成功地抓住了完整的元素从HTML页本:XPath从IMG标签解析“SRC”?
//img[@class='photo-large']
例如,它会返回此:
<img src="http://example.com/img.jpg" class='photo-large' />
但我只需要SRC URL(http://example.com/img.jpg)。任何帮助?
现在我成功地抓住了完整的元素从HTML页本:XPath从IMG标签解析“SRC”?
//img[@class='photo-large']
例如,它会返回此:
<img src="http://example.com/img.jpg" class='photo-large' />
但我只需要SRC URL(http://example.com/img.jpg)。任何帮助?
你很亲近回答这个问题,我有点不愿意为你回答。但是,以下XPath应该提供你想要的(当然,提供源代码是XHTML)。
//img[@class='photo-large']/@src
有关更多提示,请查看W3 Schools。他们对这样的事情有很好的教程,也是很好的参考。
使用角度来说,Hpricot这个工程:
doc.at('//img[@class="photo-large"]')['src']
在你有一个以上的图像的情况下,下面给出一个数组:
doc.search('//img[@class="photo-large"]').map do |e| e['src'] end
然而,引入nokogiri是many times faster,它“can be used as a drop in replacement”为Hpricot。
这里的版本引入nokogiri,在这个XPath选择属性的工作原理:
doc.at('//img[@class="photo-large"]/@src').to_s
或多幅图像:
doc.search('//img[@class="photo-large"]/@src').to_a
// IMG/@ SRC
你可以去有了这个如果你想要一个图像的链接。
例如:
<img alt="" class="avatar width-full rounded-2" height="230" src="https://avatars3.githubusercontent.com/...;s=460" width="230">
磕磕绊绊就这个问题,我不禁好奇:是什么让你回答引用红宝石相关depencies?对我来说,似乎相当不错。 – 2013-12-16 14:06:52