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
356 views
in Technique[技术] by (71.8m points)

ruby on rails - nil is not a symbol nor a string

Im trying to use Kimurai to scrape a website. Im running into this error when I want to do /scrape.

def scrape
    url = "https://www.tripadvisor.com/Restaurants-g31892-Rogers_Arkansas.html"
    response = RestaurantsScraper.parse!(response, url, data: {})
    if response[status] == :completed && response[error].nil?
      flash.now[notice] = "Successfully scraped url"
    else
      flash.now[alert] = response[error]
    end
  end

Here is my scraper class

class RestaurantsScraper < Kimurai::Base
    @name = "restaurants_scraper"
    @driver = :selenium_chrome
    @start_urls = ["https://www.tripadvisor.com/Restaurants-g31892-Rogers_Arkansas.html"]

    def parse(response, url:, data: {})
        response.xpath("//div[@class=_1llCuDZj]").each do |a|
            request_to :parse_repo_page, url: absolute_url(a[:href], base: url)
        end
    end
    
    def parse_repo_page(response, url:, data: {})
            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

Here is the error im getting error

question from:https://stackoverflow.com/questions/65924182/nil-is-not-a-symbol-nor-a-string

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

1 Reply

0 votes
by (71.8m points)

It's because response from RestaurantsScraper.parse!(response, url, data: {}) isn't defined.

From the kimurai docs it says you need to pass a Nokogiri::HTML::Document object.

I haven't used Kimurai and it feels like there is definitely a better way to do this, but something like the following may be enough to get you to the next step:

def scrape
  require 'open-uri'
  url = "https://www.tripadvisor.com/Restaurants-g31892-Rogers_Arkansas.html"  
  html = Nokogiri.parse open(url)
  response = RestaurantsScraper.parse!(html, url, data: {})
  if response[status] == :completed && response[error].nil?
    flash.now[notice] = "Successfully scraped url"
  else
    flash.now[alert] = response[error]
  end
end

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

...