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命令。
由於馬庫斯
的解決方案是:company.at_css( 「businesscategory」)兒童 – Markus 2012-03-29 16:15:13
'company'不是數組,它是一個'
回答
.at_css('.businesscategory label')
僅返回第一個匹配節點。使用.css('.businesscategory label')
得到所有匹配的節點這個XML
這個腳本
打印這個結果
所以,你可以看到,使用
.css
而不是.at_css
將解決您的問題。使用
.at_css('.businesscategory').children
將產生空白節點,所以一定要小心打印
來源
2012-03-29 16:39:27
沒有幫助...試過了,但是company.at_css(「。businesscategory」)。children did it ... – Markus 2012-03-29 18:28:39
小心,'.children'會返回所有孩子,即使是不是元素的孩子,包括空白的文本節點。 – 2012-03-29 18:42:21
相關問題