Ruby/Rails - 无法获取数组的大小
我正在使用以下代码使用 scrAPI gem 来抓取 eBay 列表:
我通过执行安装了它:
gem install scrapi
我还通过声明覆盖了它的默认文本解析器:
Scraper::Base.parser :html_parser
问题是我不断在 上收到以下错误拍卖
数组大小。不确定我做错了什么? 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
I'm using the following code to scrape an eBay listing using the scrAPI gem:
I installed this by executing:
gem install scrapi
I'm also overriding its default text parser by declaring:
Scraper::Base.parser :html_parser
The problem is that I keep receiving the following error on the auctions
array size. Not sure what I'm doing wrong? Both size
and length
don't work.
Scraper.rb:31:in `<class:ScraperDemo>': undefined method `size' for nil:NilClass (No
MethodError)Scraper.rb:10:in `<main>'
I just run via the commandline:
ruby Scraper.rb
Code:
#!/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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终选择使用 Nokogiri 作为我的刮刀。
I ended up using Nokogiri as my scraper of choice.