gpt4 book ai didi

Ruby - 遍历nokogiri元素

转载 作者:太空宇宙 更新时间:2023-11-03 18:00:22 25 4
gpt4 key购买 nike

我有一个这样的 html:

...
<table>
<tbody>
...
<tr>
<th> head </th>
<td> td1 text<td>
<td> td2 text<td>
...
</tr>
</tbody>
<tfoot>
</tfoot>
</table>

...

我将 Nokogiri 与 ruby​​ 结合使用。我想遍历每一行并将 th 和对应的 td 的文本放入哈希中。

最佳答案

require "nokogiri"

#Parses your HTML input
html_data = "...stripped HTML markup code..."
html_doc = Nokogiri::HTML html_data

#Iterates over each row in your table
#Note that you may need to clarify the CSS selector below
result = html_doc.css("table tr").inject({}) do |all, row|

#Modify if you need to collect only the first td, for example
all[row.css("th").text] = row.css("td").text

end

关于Ruby - 遍历nokogiri元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6248979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com