0
我用下面的代码使用scrAPI宝石刮易趣上市的大小:通过执行的Ruby/Rails - 无法获得阵列
我安装了这个:
gem install scrapi
我也通过宣布其覆盖默认文本解析器:
Scraper::Base.parser :html_parser
的问题是,我一直在auctions
数组大小收到以下错误。不知道我做错了什么? size
和length
都不起作用。
Scraper.rb:31:in `<class:ScraperDemo>': undefined method `size' for nil:NilClass (No
MethodError)Scraper.rb:10:in `<main>'
我只是通过命令行运行:
ruby Scraper.rb
代码:
#!/usr/bin/env ruby
require 'open-uri'
require 'httparty'
require 'json'
require 'scrapi'
Scraper::Base.parser :html_parser
class ScraperDemo
ebay_auction = Scraper.define do
process "h3.ens>a", :description=>:text, :url=>"@href"
process "td.ebcPr>span", :price=>:text
process "div.ebPicture >a>img", :image=>"@src"
result :description, :url, :price, :image
end
ebay = Scraper.define do
array :auctions
process "table.ebItemlist tr.single", :auctions=>ebay_auction
result :auctions
end
auctions = ebay.scrape(URI.parse('http://search.ebay.com/ipod-nano_W0QQcatrefZC6QQfromZR3QQfsooZ1QQfsopZ1QQkeywordZonQQsacatZQ2d1QQstrkwZipod'))
# No. of channels found
puts auctions.size # error occurs on this line number
# First auction:
auction = auctions[0]
puts auction.description
puts auction.url
end
错误消息告诉你'auctions'是'nil',而不是一个数组。问题不在'size'中。顺便说一句,由于你的错误信息给出了行号,读者更友好和礼貌的给代码提供行,或者至少告诉错误信息在哪一行。你在问这里的人是否在你的代码中计算行数? – sawa 2011-12-19 01:12:53
auctions.count是否会返回任何内容? auctions.type或auctions.class返回什么? – ScottJShea 2011-12-19 01:14:34
@ScottJShea未定义的方法'count',未定义的方法'type',auctions.class = NilClass – fuzz 2011-12-19 01:37:29