2012-03-29 48 views
0

林試圖解析標籤不與引入nokogiri一個表中的每個小區的多個標籤的表,其中有多於一個僅在一個TD字段:與引入nokogiri解析成其中存在在某一列

<tr class="alt2"> 
     <td class="company">ABB Shanghai Transformer Co., Ltd.</td> 
     <td class="contactperson">Mr. Frank Liang<br/></td> 
     <td class="businesscategory"> 
     <label><code>C27.11 </code>Manufacture of electric motors, generators and transformers</label> 
     <label><code>C27.33 </code>Manufacture of wiring devices</label> 
     </td> 
    </tr> 

所以,我現在所做的是這樣的:

doc.css("tbody tr").each do |company| 
    new = GermanSubsidiary.new 
    new.name = company.at_css(".company").text 
    new.contact = company.at_css(".contactperson").text 
    company.at_css(".businesscategory label").each do |category| 
    new_class = BusinessClassification.create 
    new_class.code = category.at_css("code").text 
    new_class.name = category.text 
    end 
end 

不幸company.at_css(".businesscategory label").each do |category|不工作,因爲at_css不數組工作...是什麼呢?

我該如何深入分析結構?由於有多行表格,我必須在女巫行的目標中加以區分,並且不能在整個文檔中使用xpath命令。

由於馬庫斯

+0

的解決方案是:company.at_css( 「businesscategory」)兒童 – Markus 2012-03-29 16:15:13

+0

'company'不是數組,它是一個''元素。當你說它「不工作」時,你的意思是什麼?將來,請不僅提供您的示例輸入和代碼(很棒),還要提供您試圖獲得的示例輸出以及實際的錯誤消息或錯誤輸出。 – Phrogz 2012-03-29 18:52:42

回答

3

.at_css('.businesscategory label')僅返回第一個匹配節點。使用.css('.businesscategory label')得到所有匹配的節點

這個XML

xml = <<-XML 
<tbody> 
    <tr class="alt2"> 
    <td class="company">ABB Shanghai Transformer Co., Ltd.</td> 
    <td class="contactperson">Mr. Frank Liang<br/></td> 
    <td class="businesscategory"> 
     <label><code>C27.11 </code>Manufacture of electric motors, generators and transformers</label> 
     <label><code>C27.33 </code>Manufacture of wiring devices</label> 
    </td> 
    </tr> 
</tbody> 
XML 

這個腳本

require 'rubygems' 
require 'nokogiri' 
require 'pp' 

doc = Nokogiri::HTML.fragment(xml) 

puts "with at_css example:" 
doc.css("tbody tr").each do |company| 
    company.at_css(".businesscategory label").each do |category| 
    puts category.at_css("code").text 
    puts category.text 
    end 
end 

puts "\n\nwith css" 
doc.css("tbody tr").each do |company| 
    company.css(".businesscategory label").each do |category| 
    puts category.at_css("code").text 
    puts category.text 
    end 
end 

打印這個結果

with at_css example: 


with css 
C27.11 
C27.11 Manufacture of electric motors, generators and transformers 
C27.33 
C27.33 Manufacture of wiring devices 

所以,你可以看到,使用.css而不是.at_css將解決您的問題。

使用.at_css('.businesscategory').children將產生空白節點,所以一定要小心

puts "\n\nwith at_css().children" 
doc.css("tbody tr").each do |company| 
    company.at_css(".businesscategory").children.each do |category| 
    puts category.text.inspect 
    end 
end 

打印

with at_css().children 
"\n  " 
"C27.11 Manufacture of electric motors, generators and transformers" 
"\n  " 
"C27.33 Manufacture of wiring devices" 
"\n " 
+0

沒有幫助...試過了,但是company.at_css(「。businesscategory」)。children did it ... – Markus 2012-03-29 18:28:39

+0

小心,'.children'會返回所有孩子,即使是不是元素的孩子,包括空白的文本節點。 – 2012-03-29 18:42:21