Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
737 views
in Technique[技术] by (71.8m points)

ruby on rails - Error: wrong number of arguments (given 0, expected 2)

Im trying to make a basic web scraper with Rails. everytime I hit the scrape button it sends me to the correct location but gives me this error everytime.

here is my restaurants_controller.rb file

  def scrape
    url = 'https://www.tripadvisor.com/Restaurants-g31892-Rogers_Arkansas.html'
    response = RestaurantsScraper.process(url)
    if response[:status] == :completed && response[:error].nil?
      flash.now[:notice] = "Successfully scraped url"
    else
      flash.now[:alert] = response[:error]
    end
  rescue StandardError => e
    flash.now[:alert] = "Error: #{e}"
  end

Here is my restaurantscrapper.rb file

class RestaurantsScraper < Kimurai::Base
    @name = 'restaurants_scraper'
    @engine = :mechanize


    def self.process(url)
        @start_url = [url]
        self.crawl!
    end


    def parse(response, url, data:{})
        response.xpath("//div[@class=_1llCuDZj]").each do |t|
            item = {}
            
            item[:title] = t.css('a._15_ydu6b')&.text&.squish&.gsub('[^0-9].', '')
            item[:type] = t.css('span._1p0FLy4t')&.text&.squish
            item[:reviews] = t.css('span.w726Ki5B').text&.squish
            item[:top_reviews] = t.css('a._2uEVo25r _3mPt7dFq').text&.squish

            Restaurant.where(item).first_or_create
        end
    end

end

Here is a picture of the error im getting Error

Here is the error in the console

Processing by RestaurantsController#scrape as HTML
  Parameters: {"authenticity_token"=>"3qXvtTOsU6VVtxaPvNXyCjpdnHLOgCvFgQYzB1JnhoHDz8ySF6gK/n5x+/XW5HC0HwfzQ1bFCu/KCfF3nA1SIQ=="}
I, [2021-01-26 02:35:35 -0600#218] [C: 14760]  INFO -- restaurants_scraper: Spider: started: restaurants_scraper
F, [2021-01-26 02:35:35 -0600#218] [C: 14760] FATAL -- restaurants_scraper: Spider: stopped: {:spider_name=>"restaurants_scraper", :status=>:failed, :error=>"#<ArgumentError: wrong number of arguments (given 0, expected 2)>", :environment=>"development", :start_time=>2021-01-26 02:35:35.6330106 -0600, :stop_time=>2021-01-26 02:35:35.6336933 -0600, :running_time=>"0s", :visits=>{:requests=>0, :responses=>0}, :items=>{:sent=>0, :processed=>0}, :events=>{:requests_errors=>{}, :drop_items_errors=>{}, :custom=>{}}}
  Rendering restaurants/scrape.html.erb within layouts/application
  Rendered restaurants/scrape.html.erb within layouts/application (Duration: 0.1ms | Allocations: 41)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 45ms (Views: 41.9ms | ActiveRecord: 0.0ms | Allocations: 4104)

question from:https://stackoverflow.com/questions/65898150/error-wrong-number-of-arguments-given-0-expected-2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a typo. It should be @start_urls = [url] - plural. The error is caused by line 131 in the crawl method. See the snippet:

  spider = self.new

  if start_urls
    ...
  else
    spider.parse
  end

Since start_urls is not defined it goes to the else branch and calls parse method that you defined, which requires two arguments but it's called with 0 as the error says.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...