我想将我用 open-uri 和 nokogiri 抓取的 HTML 文件填充的 Ruby 变量放入反引号系统进程中以对其进行整理。变量的性质混淆了过程。我想我需要逃避它,但我不确定。任何建议表示赞赏。
require 'open-uri'
require 'nokogiri'
url = 'http://www.wikihow.com/Bathe-a-Cat'
page = Nokogiri::HTML(open(url))
pagestring = page.to_s
result = `tidy --break-before-br no --char-encoding utf8 --clean yes --drop-empty-paras yes ' #{pagestring}'`
puts results.length
这是我得到的错误:
sh: -c: line 144: syntax error near unexpected token `"Search","Search","Custom_search"'
sh: -c: line 144: ` <input type="submit" id="cse_sa" value="Search" class="search_button" onmouseover="button_swap(this);" onmouseout="button_unswap(this);" onclick='gatTrack("Search","Search","Custom_search");'>'
干杯
最佳答案
您可能想改用 IO.popen。然后您可以使用数组调用命令而不是将其字符串化:
cmd = %w{ tidy --break-before-br no --char-encoding utf8 --clean yes --drop-empty-paras yes }
result = IO.popen(cmd, 'r+') {|io|
io.puts pagestring
io.close_write
io.read
}
假设 tidy
从标准输入读取 HTML。
关于Ruby 在反引号 Shell 中转义参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27623072/