Ruby/Rails - 无法获取数组的大小

发布于 2024-12-22 05:23:07 字数 1490 浏览 0 评论 0原文

我正在使用以下代码使用 scrAPI gem 来抓取 eBay 列表:

我通过执行安装了它:

gem install scrapi

我还通过声明覆盖了它的默认文本解析器:

Scraper::Base.parser :html_parser

问题是我不断在 上收到以下错误拍卖数组大小。不确定我做错了什么? sizelength 都不起作用。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声静候 2024-12-29 05:23:07

我最终选择使用 Nokogiri 作为我的刮刀。

I ended up using Nokogiri as my scraper of choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文