我正在尝试使用 Nokogiri 从多个供应商处获取商品的名称和价格。我通过方法参数将 CSS 选择器(查找名称和价格)传递给 Nokogiri。
有关如何将多个 URL 传递给“scrape”方法同时还传递其他参数(例如:供应商、item_path)的任何指导?还是我以完全错误的方式解决这个问题?
这是代码:
require 'rubygems' # Load Ruby Gems
require 'nokogiri' # Load Nokogiri
require 'open-uri' # Load Open-URI
@@collection = Array.new # Array to hold meta hash
def scrape(url, vendor, item_path, name_path, price_path)
doc = Nokogiri::HTML(open(url)) # Opens URL
items = doc.css(item_path) # Sets items
items.each do |item| # Iterates through each item on grid
@@collection << meta = Hash.new # Creates a new hash then add to global array
meta[:vendor] = vendor
meta[:name] = item.css(name_path).text.strip
meta[:price] = item.css(price_path).to_s.scan(/\d+[.]\d+/).join
end
end
scrape( "page_a.html", "Sample Vendor A", "#products", ".title", ".prices")
scrape( ["page_a.html", "page_b.html"], "Sample Vendor B", "#items", ".productname", ".price")