Ruby - 无法使用 nokogiri 写入
Ruby - unable to write using nokogiri
我正在按类名搜索 <div>
元素,并想将其添加到其他 <div>
元素旁边。以下代码没有写入我使用 doc1.search 获得的数据。
require 'nokogiri'
doc1 = Nokogiri::HTML(File.open("overview.html"))
affixButtons = doc1.search('div.margin-0-top-lg.margin-10-bottom-lg.text-center')
doc1.at('div.leftnav-btn').add_next_sibling(affixButtons)
有人可以建议我缺少什么吗?
您的代码工作得很好,如果您只想将编辑后的数据写入文件,请使用 File.open
,如下所示:
require 'nokogiri'
doc1 = Nokogiri::HTML(File.open("overview.html"))
affixButtons = doc1.search('div.margin-0-top-lg.margin-10-bottom-lg.text-center')
doc1.at('div.leftnav-btn').add_next_sibling(affixButtons)
File.open('output.html', 'w') {|f| f.write(doc1.to_html)}
您不会将结果 HTML 保存到文件中,您可以这样做:
File.open("result.html", "w"){|f| f.write(doc1.to_html)}
我正在按类名搜索 <div>
元素,并想将其添加到其他 <div>
元素旁边。以下代码没有写入我使用 doc1.search 获得的数据。
require 'nokogiri'
doc1 = Nokogiri::HTML(File.open("overview.html"))
affixButtons = doc1.search('div.margin-0-top-lg.margin-10-bottom-lg.text-center')
doc1.at('div.leftnav-btn').add_next_sibling(affixButtons)
有人可以建议我缺少什么吗?
您的代码工作得很好,如果您只想将编辑后的数据写入文件,请使用 File.open
,如下所示:
require 'nokogiri'
doc1 = Nokogiri::HTML(File.open("overview.html"))
affixButtons = doc1.search('div.margin-0-top-lg.margin-10-bottom-lg.text-center')
doc1.at('div.leftnav-btn').add_next_sibling(affixButtons)
File.open('output.html', 'w') {|f| f.write(doc1.to_html)}
您不会将结果 HTML 保存到文件中,您可以这样做:
File.open("result.html", "w"){|f| f.write(doc1.to_html)}