【问题标题】:Iterating through multiple URLs to parse HTML with Nokogori使用 Nokogori 遍历多个 URL 以解析 HTML
【发布时间】:2013-03-16 18:38:27
【问题描述】:

我正在尝试使用 Nokogiri 从多个供应商处获取商品的名称和价格。我通过方法参数将 CSS 选择器(查找名称和价格)传递给 Nokogiri。

关于如何将多个 URL 传递给“scrape”方法同时传递其他参数(例如:供应商、i​​tem_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")

【问题讨论】:

    标签: ruby nokogiri open-uri


    【解决方案1】:

    您可以传递多个url's,就像您在第二个示例中所做的那样:

    scrape( ["page_a.html", "page_b.html"], "Sample Vendor B",  "#items", ".productname", ".price")
    

    您的scrape 方法必须遍历那些urls,例如:

    def scrape(urls, vendor, item_path, name_path, price_path)
      urls.each do |url|
        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   
    end
    

    这也意味着第一个例子也需要作为数组传递:

    scrape( ["page_a.html"], "Sample Vendor A", "#products", ".title", ".prices")
    

    【讨论】:

    • 谢谢!这很好用。我不知道我还必须将第一个示例作为数组传递。
    【解决方案2】:

    仅供参考,使用@@collection 是不合适的。相反,编写您的方法以返回一个值:

    def scrape(urls, vendor, item_path, name_path, price_path)
      collection = []
      urls.each do |url|
        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 << {
            :vendor => vendor,
            :name   => item.css(name_path).text.strip,
            :price  => item.css(price_path).to_s.scan(/\d+[.]\d+/).join
          }
        end 
      end   
    
      collection
    end
    

    可以简化为:

    def scrape(urls, vendor, item_path, name_path, price_path)
      urls.map { |url|
        doc = Nokogiri::HTML(open(url)) # Opens URL
        items = doc.css(item_path) # Sets items
        items.map { |item| # Iterates through each item on grid
          {
            :vendor => vendor,
            :name   => item.css(name_path).text.strip,
            :price  => item.css(price_path).to_s.scan(/\d+[.]\d+/).join
          }
        } 
      }
    end
    

    【讨论】:

    • 使用您建议的第二种方法只是将我的实际执行时间减少了一半。使用我的原始代码,我使用它来打印数据:def list_matches @@collection.each do |k| puts "Vendor: #{k[:vendor]}" puts "Name: #{k[:name]}" puts "Price: $#{k[:price]}" end end 我怎么能用你的方法做同样的事情?感谢您的帮助。
    • 嘿,奇迹会不会停止?我只能说有时我很幸运。 :-) 实际上,我们在编码中一次又一次地重用了一些模式,而且,随着时间的推移,我们应该学会识别代码的某些用途/应用程序,并立即使用它们。第二个是第一个的缩减,是多年使用多种不同语言编写代码的结果。能帮上忙真是太好了。
    猜你喜欢
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 2013-02-28
    • 2022-09-23
    • 1970-01-01
    • 2012-05-14
    • 2010-11-04
    • 1970-01-01
    相关资源
    最近更新 更多