【问题标题】:Web Scraping with Nokogiri::HTML and Ruby - How to get output into an array?使用 Nokogiri::HTML 和 Ruby 进行 Web Scraping - 如何将输出放入数组?
【发布时间】:2013-03-17 16:02:25
【问题描述】:

我刚开始使用 nokogiri 从网站上抓取信息,但不知道如何完成以下工作。我有一些 HTML 代码要抓取:

    <div class="compatible_vehicles">
    <div class="heading">
    <h3>Compatible Vehicles</h3>
    </div><!-- .heading -->
    <ul>
            <li>
        <p class="label">Type1</p>
        <p class="data">All</p>
    </li>
    <li>
        <p class="label">Type2</p>
      <p class="data">All</p>
    </li>
    <li>
        <p class="label">Type3</p>
      <p class="data">All</p>
    </li>
    <li>
        <p class="label">Type4</p>
      <p class="data">All</p>
    </li>
    <li>
        <p class="label">Type5</p>
      <p class="data">All</p>
    </li>
    </ul>
    </div><!-- .compatible_vehicles -->

我已经设法在我的屏幕上获得了我想要的输出:

    i = 0
     doc.css('div > .compatible_vehicles > ul > li').each do |item|  
      label = item.at_css(".label").text
      data = item.at_css(".data").text
     print "#{label} - #{data}" + ','
    end  
    i += 1

这给了我一个这样的列表:Type1 - All,Type2 - All,Type3 - All,Type4 - All,Type5 - All, 在屏幕上。

现在我想在一个数组中获取这个值,以便能够将它保存到一个 CSV 文件中。我尝试了几件事,但大多数尝试都收到“无法将字符串转换为数组”错误。 希望有人能帮我解决这个问题!

【问题讨论】:

    标签: ruby nokogiri scrape


    【解决方案1】:

    从 HTML 开始:

    html = '
    <div class="compatible_vehicles">
        <div class="heading">
            <h3>Compatible Vehicles</h3>
        </div><!-- .heading -->
        <ul>
            <li>
            <p class="label">Type1</p>
            <p class="data">All</p>
            </li>
            <li>
            <p class="label">Type2</p>
            <p class="data">All</p>
            </li>
            <li>
            <p class="label">Type3</p>
            <p class="data">All</p>
            </li>
            <li>
            <p class="label">Type4</p>
            <p class="data">All</p>
            </li>
            <li>
            <p class="label">Type5</p>
            <p class="data">All</p>
            </li>
        </ul>
    </div><!-- .compatible_vehicles -->
    '
    

    用 Nokogiri 解析它并遍历 &lt;li&gt; 标签以获得它们的 &lt;p&gt; 标签内容:

    require 'nokogiri'
    
    doc = Nokogiri::HTML(html)
    data = doc.search('.compatible_vehicles li').map{ |li|
      li.search('p').map { |p| p.text }
    }
    

    返回一个数组数组:

    => [["Type1", "All"], ["Type2", "All"], ["Type3", "All"], ["Type4", "All"], ["Type5", "All"]]
    

    从那里您应该能够将其插入到 CSV 类的示例中,并让它毫无问题地工作。

    现在,将要输出到屏幕的字段的代码与以下内容进行比较:

    data.map{ |a| a.join(' - ') }.join(', ')
    => "Type1 - All, Type2 - All, Type3 - All, Type4 - All, Type5 - All"
    

    我所要做的就是puts,它会正确打印。

    考虑返回有用的数据结构非常重要。在 Ruby 中,哈希和数组非常有用,因为我们可以遍历它们并将它们按摩成多种形式。从数组的数组中创建一个哈希是微不足道的:

    Hash[data]
    => {"Type1"=>"All", "Type2"=>"All", "Type3"=>"All", "Type4"=>"All", "Type5"=>"All"}
    

    这将使查找变得非常容易。

    【讨论】:

    • 太好了,开始了!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2017-05-14
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多