Ruby scraper. How to export to CSV?

2019-08-03 07:59发布

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)

I do not understand this piece of code. What's this doing and why isn't it working right?

  send_data csv_data, 
            :type => 'text/csv; charset=iso-8859-1; header=present', 
            :disposition => "attachment; filename=products.csv" 

Full 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 << ["sku", "name", "desc"] 

    # data rows 
    productsArray.each do |product| 
      csv << [product.sku, product.name, product.desc] 
    end 
  end 

  send_data csv_data, 
            :type => 'text/csv; charset=iso-8859-1; header=present', 
            :disposition => "attachment; filename=products.csv" 

3条回答
来,给爷笑一个
2楼-- · 2019-08-03 08:21

It probably makes more sense to do:

@csv = FasterCSV.open('filename.csv', 'w')

and then write to it as you go along:

@csv << [sku, name, desc]

that way if your script crashes halfway through you've at least got half of the data.

查看更多
做个烂人
3楼-- · 2019-08-03 08:29

If you are new to Ruby, you should be using Ruby 1.9 or later, in which case you can use the built-in CSV output which builds in fast csv plus l18n support:

require 'csv'
CSV.open('filename.csv', 'w') do |csv|
  csv << [sku, name, desc]
end

http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html

查看更多
放荡不羁爱自由
4楼-- · 2019-08-03 08:35
File.open('filename.csv', 'w') do |f|
  f.write(csv_data)
end
查看更多
登录 后发表回答