Question

I'm a complete Ruby noob, currently going through the Treehouse tutorials but I need some quick help for outputting the content of an Anemone crawl into a text file for my job(I'm an SEO). How do I get the following to dump it's output into a text file?

require 'anemone'

Anemone.crawl("http://www.example.com/") do |anemone|
 anemone.on_every_page do |page|
   puts page.url
 end
end

Help is much appreciated!

Was it helpful?

Solution

You can puts to a file handle, almost the same as if it was STDOUT. A very simple adjustment to your code is to add a File.open block:

require 'anemone'

File.open('report.txt', 'w') do |file|

  Anemone.crawl("http://www.example.com/") do |anemone|
   anemone.on_every_page do |page|
     file.puts page.url
   end
  end

end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top