从a href html标记中提取链接(URL),使用ruby中的nokogiri?

从a href html标记中提取链接(URL),使用ruby中的nokogiri?,ruby,parsing,nokogiri,Ruby,Parsing,Nokogiri,我想从网页中提取所有URL我如何使用nokogiri做到这一点 例如: <div class="heat"> <a href='http://example.org/site/1/'>site 1</a> <a href='http://example.org/site/2/'>site 2</a> <a href='http://example.org/site/3/'>site 3</a>

我想从网页中提取所有URL我如何使用nokogiri做到这一点

例如:

<div class="heat"> <a href='http://example.org/site/1/'>site 1</a> <a href='http://example.org/site/2/'>site 2</a> <a href='http://example.org/site/3/'>site 3</a> </diV> 结果应该是一个列表:


l=['',''

您可以这样做:

doc = Nokogiri::HTML.parse(<<-HTML_END)
<div class="heat">
   <a href='http://example.org/site/1/'>site 1</a>
   <a href='http://example.org/site/2/'>site 2</a>
   <a href='http://example.org/site/3/'>site 3</a>
</div>
<div class="wave">
   <a href='http://example.org/site/4/'>site 4</a>
   <a href='http://example.org/site/5/'>site 5</a>
   <a href='http://example.org/site/6/'>site 6</a>
</div>
HTML_END

l = doc.css('div.heat a').map { |link| link['href'] }

doc=Nokogiri::HTML.parse(ok)多亏了sris,这段代码非常适合我

p doc.xpath('//div[@class="heat"]/a').map { |link| link['href'] }
p doc.xpath('//div[@class=“heat”]/a').map{{| link | link['href']}非常好,你救了我一天,谢谢:)有一件事我现在从网站上获得了所有链接,但我只想要div内带有clas“heat”的链接。这也是可能的吗?这个例子也适用于中的任何链接,而不是活动链接吗?@AaronMoodie:是的,它适用于类为
heat
的div中的任何链接。什么是活动链接?是否有人有使用正则表达式的类似解决方案?是否有速度不同? p doc.xpath('//div[@class="heat"]/a').map { |link| link['href'] }