简体   繁体   English

Ruby-无法使用nokogiri编写

[英]Ruby - unable to write using nokogiri

I am searching a <div> element by its classname and want to add it next to other <div> element. 我正在按其类名搜索<div>元素,并想将其添加到其他<div>元素旁边。 Following code is not writing data what I get using doc1.search . 下面的代码没有写数据,我使用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)

Can someone suggest what I'm missing ? 有人可以建议我缺少什么吗?

Your code works just fine, if you would just like to write the edited data to file use File.open as follows: 如果您只想使用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)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM