Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby scraper. How to export to CSV?
    primarykey
    data
    text
    <p>I wrote this ruby script to scrape product info from the manufacturer website. The scraping and storage of the product objects in an array works, but I can't figure out how to export the array data to a csv file. This error is being thrown: scraper.rb:45: undefined method `send_data' for main:Object (NoMethodError)</p> <p>I do not understand this piece of code. What's this doing and why isn't it working right?</p> <pre><code> send_data csv_data, :type =&gt; 'text/csv; charset=iso-8859-1; header=present', :disposition =&gt; "attachment; filename=products.csv" </code></pre> <p>Full code:</p> <pre><code>#!/usr/bin/ruby require 'rubygems' require 'anemone' require 'fastercsv' productsArray = Array.new class Product attr_accessor :name, :sku, :desc end # Scraper Code Anemone.crawl("http://retail.pelicanbayltd.com/") do |anemone| anemone.on_every_page do |page| currentPage = Product.new #Product info parsing currentPage.name = page.doc.css(".page_headers").text currentPage.sku = page.doc.css("tr:nth-child(2) strong").text currentPage.desc = page.doc.css("tr:nth-child(4) .item").text if currentPage.sku =~ /#\d\d\d\d/ currentPage.sku = currentPage.sku[1..-1] productsArray.push(currentPage) end end end # CSV Export Code products = productsArray.find(:all) csv_data = FasterCSV.generate do |csv| # header row csv &lt;&lt; ["sku", "name", "desc"] # data rows productsArray.each do |product| csv &lt;&lt; [product.sku, product.name, product.desc] end end send_data csv_data, :type =&gt; 'text/csv; charset=iso-8859-1; header=present', :disposition =&gt; "attachment; filename=products.csv" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload