title: web scraping/export to CSV with Ruby
body: <p>ruby n00b here in hope of some guidance. I am looking to scrape a website (600-odd names and links on one page) and output to CSV. The scraping itself works fine (the output correctly fills the terminal as the script runs), but I can't get the CSV to populate. The code:</p>
  
  <pre><code>   require 'rubygems'
     require 'nokogiri'   
     require 'open-uri'
     require 'csv'
  
     url = "http://www.example.com/page/"
     page = Nokogiri::HTML(open(url))
  
     page.css('.item').each do |item|
       name = item.at_css('a').text
       link = item.at_css('a')[:href]
       foo = puts "#{name}"
       bar = "#{link}"
  
       CSV.open("file.csv", "wb") do |csv|
         csv &lt;&lt; [foo, bar]
       end
     end
  
     puts "upload complete!"
  </code></pre>
  
  <p>...replacing the <code>csv &lt;&lt; [foo, bar]</code> with <code>csv &lt;&lt; [name, link]</code> just puts the final iteration into the CSV. I feel there's something basic I am missing here. Thanks for reading.</p>