如何废弃<li>和儿童的文字

时间:2016-02-08 13:12:51

标签: ruby web-scraping nokogiri

我正在尝试抓取<li>代码的内容。

HTML看起来像:

 <div class="insurancesAccepted">
   <h4>What insurance does he accept?*</h4>
   <ul class="noBottomMargin">
      <li class="first"><span>Aetna</span></li>
      <li>
         <a title="See accepted plans" class="insurancePlanToggle arrowUp">AvMed</a>
         <ul style="display: block;" class="insurancePlanList">
            <li class="last first">Open Access</li>
         </ul>
      </li>
      <li>
         <a title="See accepted plans" class="insurancePlanToggle arrowUp">Blue Cross Blue Shield</a>
         <ul style="display: block;" class="insurancePlanList">
            <li class="last first">Blue Card PPO</li>
         </ul>
      </li>
      <li>
         <a title="See accepted plans" class="insurancePlanToggle arrowUp">Cigna</a>
         <ul style="display: block;" class="insurancePlanList">
            <li class="first">Cigna HMO</li>
            <li>Cigna PPO</li>
            <li class="last">Great West Healthcare-Cigna PPO</li>
         </ul>
      </li>
      <li class="last">
         <a title="See accepted plans" class="insurancePlanToggle arrowUp">Empire Blue Cross Blue Shield</a>
         <ul style="display: block;" class="insurancePlanList">
            <li class="last first">Empire Blue Cross Blue Shield HMO</li>
         </ul>
      </li>
   </ul>
  </div>

主要问题是我试图从以下内容获取内容:

doc.css('.insurancesAccepted li').text.strip

一次显示所有<li>文字。我想要&#34; AvMed&#34;和&#34;开放存取&#34;使用关系参数同时报废,以便我可以通过引用将其插入到我的MySQL表中。

1 个答案:

答案 0 :(得分:1)

问题是doc.css('.insurancesAccepted li')匹配所有嵌套列表项,而不仅仅是直接后代。要仅匹配直接后代,应使用parent > child CSS规则。要完成任务,您需要仔细组合迭代的结果:

doc = Nokogiri::HTML(html)
result = doc.css('div.insurancesAccepted > ul > li').each do |li|
  chapter = li.css('span').text.strip
  section = li.css('a').text.strip
  subsections = li.css('ul > li').map(&:text).map(&:strip)

  puts "#{chapter} ⇒ [ #{section} ⇒ [ #{subsections.join(', ')} ] ]"
  puts '=' * 40
end

导致:

# Aetna ⇒ [  ⇒ [  ] ]
# ========================================
#  ⇒ [ AvMed ⇒ [ Open Access ] ]
# ========================================
#  ⇒ [ Blue Cross Blue Shield ⇒ [ Blue Card PPO ] ]
# ========================================
#  ⇒ [ Cigna ⇒ [ Cigna HMO, Cigna PPO, Great West Healthcare-Cigna PPO ] ]
# ========================================
#  ⇒ [ Empire Blue Cross Blue Shield ⇒ [ Empire Blue Cross Blue Shield HMO ] ]
# ========================================