【问题标题】:How to get links using mechanize and nokogiri ruby如何使用 mechanize 和 nokogiri ruby​​ 获取链接
【发布时间】:2015-04-17 18:27:18
【问题描述】:

鉴于下面的示例,谁能告诉我如何使用 Nokogiri 和 Mechanize 将每个 <h4> 标签下的所有链接放在不同的组中,即以下所有链接:

  1. “一些文字”
  2. “还有一些文字”
  3. “一些额外的文字”
<div id="right_holder">
    <h3><a href="#"><img src="http://example.com" width="11" height="11"></a></h3>
    <br />
    <br />
    <h4><a href="#">Some text</a></h4>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <br />
    <br />
    <h4><a href="#">Some more text</a></h4>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <br />
    <br />
    <h4><a href="#">Some additional text</a></h4>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
    <a href="#" alt="name of item"><img src="http://some.image.com" class="class1"></a>
</div>

【问题讨论】:

    标签: ruby web-scraping nokogiri mechanize


    【解决方案1】:

    通常你会这样做:

    page.search('h4 a').each do |a|
      puts a[:href]
    end
    

    但我相信您已经注意到这些链接实际上都没有到达任何地方。

    更新:

    要对它们进行分组,一些节点集数学如何:

    page.search('h4').each do |h4|
      puts h4.text
      (h4.search('~ a') - h4.search('~ h4 ~ a')).each do |a|
        puts a.text
      end
    end
    

    这意味着每个a 都跟随h4 并且不跟随另一个h4

    【讨论】:

    • 我认为@akhanaton 想要每个h4 a 下的链接而不是实际的h4 a 链接。
    • @akhanton,在这种情况下是:h4 ~ a
    • 这会获取所有链接,但不会根据

      标签将它们分开,我需要知道每个链接来自哪个

      标签。谢谢

    【解决方案2】:

    您可以检查并分离“How to split a HTML document using Nokogiri?”之类的数据,但如果您知道标签将是什么,您可以只使用split 它:

    # html is the raw html string
    html.split('<h4').map{|g| Nokogiri::HTML::DocumentFragment.parse(g).css('a') }
    

    page = Nokogiri::HTML(html).css("#right_holder")
    links = page.children.inject([]) do |link_hash, child|
      if child.name == 'h4'
        name = child.text
        link_hash << { :name => name, :content => ""}
      end
    
      next link_hash if link_hash.empty?
      link_hash.last[:content] << child.to_xhtml
      link_hash
    end
    
    grouped_hsh = links.inject({}) do |hsh, link|
      hsh[link[:name]] = Nokogiri::HTML::DocumentFragment.parse(link[:content]).css('a')
      hsh
    end
    
    # {"Some text"=>[#<Nokogiri::XML::Element:0x3ff4860d6c30,
    #  "Some more text"=>[#<Nokogiri::XML::Element:0x3ff486096c20...,
    #  "Some additional text"=>[#<Nokogiri::XML::Element:0x3ff486f2de78...}
    

    【讨论】:

    • 这会获取所有链接,但不会根据

      标签将它们分开,我需要知道每个链接来自哪个

      标签。谢谢

    • 我更新了我的解决方案以遵循我链接的策略。我的原始解决方案将h4 a 链接作为数组中的第一个链接,但它还包括h4s 之前的任何链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    相关资源
    最近更新 更多