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!

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top