【问题标题】:I have a name error and not exactly sure where to define my argument我有一个名称错误,不确定在哪里定义我的论点
【发布时间】:2020-05-04 01:06:47
【问题描述】:

我正在制作一个吉他浏览 CLI 项目。我有不同吉他的名称和网址,如下所示:

def self.get_electric
        doc = Nokogiri::HTML(open("https://reverb.com/c/electric-guitars"))

        electrics = []
        counter = 0

        while counter < doc.css("h2:contains('Popular Electric Guitars')+div.overflowing-row__items ul.tiles.tiles--single-row.tiles--grow.tiles--three-wide li.tiles__tile").length
        electric = {
            name: doc.css("h2:contains('Popular Electric Guitars')+div.overflowing-row__items ul.tiles.tiles--single-row.tiles--grow.tiles--three-wide li.tiles__tile")[counter].text,
            url: doc.css("h2:contains('Popular Electric Guitars')+div.overflowing-row__items ul.tiles.tiles--single-row.tiles--grow.tiles--three-wide li.tiles__tile a.marketing-callout__inner")[counter]["href"]
        }
        counter += 1
        electrics << electric
        end
        electrics
        HiStrung::Guitar.mass_create_electrics(electrics)
    end

如果用户输入“电”或“原声”,他们将获得吉他的名称和网址:

Enter here: electric

1. Fender Telecaster - https://reverb.com/marketplace/electric-guitars?query=telecaster
2. Gibson Les Paul - https://reverb.com/marketplace?query=les%20paul
3. Fender Stratocaster - https://reverb.com/marketplace/electric-guitars?query=stratocaster
4. Gibson SG - https://reverb.com/marketplace?query=sg
5. Fender Jazzmaster - https://reverb.com/marketplace?query=jazzmaster

现在我希望我的程序做的是,当用户输入他们想了解的任何吉他的编号时,它会显示该吉他的价格和描述。价格和描述在另一个网址中。

我正在尝试制作一种方法来获取 url 并获取价格和描述的 html:

def self.get_electric_info(electric)
        url = electric.url
        doc = Nokogiri::HTML(url)
    end

但我收到此名称错误:

pry(main)> HiStrung::Scraper.get_electric_info(electric)
NameError: undefined local variable or method `electric' for main:Object
from (pry):1:in `__pry__'

这是我的初始化类:

class HiStrung::Guitar
attr_accessor :name, :url

@@electrics = []
@@acoustics = []

def self.electrics
    @@electrics
end

def self.acoustics
    @@acoustics
end

def self.mass_create_electrics(electric_hash)
    electric_hash.each do |e_hash|
        electric = HiStrung::Guitar.new(e_hash[:name], e_hash[:url])
        @@electrics << electric
    end
end

def self.mass_create_acoustics(acoustic_hash)
    acoustic_hash.each do |a_hash|
        acoustic = HiStrung::Guitar.new(a_hash[:name], a_hash[:url])
        @@acoustics << acoustic
    end
end

def initialize(name, url)
    @name, @url = name, url
end

结束

【问题讨论】:

    标签: ruby web-scraping command-line-interface


    【解决方案1】:

    您没有在调用该方法时提供上下文,但是如果您查看代码,您会发现您没有定义electric 变量。

    在调用方法之前,将 electric 分配给您想要获取信息的 Guitar 对象。

    做这样的事情:

    electric = HiStrung::Guitar.new("My name", "My url")
    HiStrung::Scraper.get_electric_info(electric)
    

    这应该可以解决问题。

    【讨论】:

    • 我做到了:electric = HiStrung::Guitar.new(name, url) def self.get_electric_info(electric) url = electric.url doc = Nokogiri::HTML(open(url)) end
    • 但我得到了:/Users/tlreigns/RoyalTerminal/hi_strung/lib/hi_strung/scraper.rb:57:in ``&lt;class:Scraper&gt;'``: uninitialized constant HiStrung::Guitar (NameError) 我在我的环境中需要它。
    • 如果没有完整的上下文,这有点难以判断,但听起来你需要仔细检查你的 require 和 require_relative 调用是否正确地完成了那里的工作,并且它们在正确的位置。
    猜你喜欢
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2017-02-13
    • 1970-01-01
    • 2021-03-10
    • 2014-05-05
    相关资源
    最近更新 更多