如何检索<a href=""> from a page and show on Rails page?

时间:2017-04-05 06:34:31

标签: html ruby-on-rails ruby web-scraping

I have been given ASP and HTML files which contain HTML and some JavaScript code. I have to write a Rails application which will retrieve links from those pages and show them in Rails views.

I am stuck. Will Nokogiri help?

1 个答案:

答案 0 :(得分:1)

是的,它会。

# open and read your HTML file as Nokogiri::HTML document
doc = File.open("your_file.html") { |f| Nokogiri::HTML(f) }

# collect all links that have not empty href attribute
links = doc.css('a').map { |link| link['href'] }.reject { |link| link.blank? }