使用Ruby将HTML作为格式化文本呈现给标准输出

使用Ruby将HTML作为格式化文本呈现给标准输出,ruby,nokogiri,Ruby,Nokogiri,在构建CLI谷歌卡片查看器时,我偶然发现了在命令行中呈现HTML的问题,比如浏览器w3m或lynx。我最近一次使用的是从Nokogiri吐出的文本: Nokogiri::HTML::parse(卡片片段)。text 但它打印出来如下: "Albert EinsteinTheoretical PhysicistAlbert Einstein was a German-born theoretical physicist. He developed the general theory of rel

在构建CLI谷歌卡片查看器时,我偶然发现了在命令行中呈现HTML的问题,比如浏览器w3mlynx。我最近一次使用的是从Nokogiri吐出的文本:

Nokogiri::HTML::parse(卡片片段)。text

但它打印出来如下:

"Albert EinsteinTheoretical PhysicistAlbert Einstein was a German-born theoretical physicist. He developed the general theory of relativity, one of the two pillars of modern physics. Einstein's work is also known for its influence on the philosophy of science. WikipediaBorn: March 14, 1879, Ulm, GermanyDied: April 18, 1955, Princeton, New Jersey, United StatesInfluenced: Satyendra Nath Bose, Wolfgang Pauli, Leo Szilard, moreInfluenced by: Isaac Newton, Mahatma Gandhi, moreBooksThe World as I See It1949Relativity: The Special a...1916Ideas and Opinions2000Out of My Later Years2006The Meaning of Relativity1922The Evolution of Physics1938People also search forIsaac NewtonEduard EinsteinSonStephen HawkingElsa EinsteinSpouseMileva MarićFormer spouseThomas Edison"
但是使用lynx:

cat card_snippet.html | lyx-dump-stdin

Albert Einstein
Theoretical Physicist
Albert Einstein was a German-born theoretical physicist. He
developed the general theory of relativity, one of the two pillars
of modern physics. Einstein's work is also known for its influence
on the philosophy of science. Wikipedia
Born: March 14, 1879, Ulm, Germany
Died: April 18, 1955, Princeton, New Jersey, United States
Influenced: Satyendra Nath Bose, Wolfgang Pauli, Leo
Szilard,
注意:去除一些噪音后。但尽管如此,行尾还是合适的

在Ruby中有类似的解决方案吗?html代码片段:。

这对我很有用

require 'nokogiri'
html = `curl http://pastebin.com/raw/pYKwACBp`
doc = Nokogiri::HTML(html)
puts doc.text.gsub(/[\r\n]+/,"\n").strip
这对我有用

require 'nokogiri'
html = `curl http://pastebin.com/raw/pYKwACBp`
doc = Nokogiri::HTML(html)
puts doc.text.gsub(/[\r\n]+/,"\n").strip

你的问题用词不太合适。你基本上是要求我们推荐一些东西,或者就如何写一些东西给出我们的意见,但这会导致一个教程。相反,你应该确定你想做什么,试着写下来,然后当你遇到问题时,问一个关于这个问题的具体问题。请同时阅读“”;数据(HTML)需要在问题本身中,使用演示问题所需的最小值。链接腐烂然后断开,导致产生毫无意义的问题。您看到的问题是由于在节点集上使用了
文本。您需要迭代所有元素并分别提取它们的文本,这样才更有意义。用ruby运行系统ckmmand是否足以捕获w3m输出?@maxpleaner这是可能的,但我想知道是否可以消除这种依赖关系。@theTinMan okay下次会注意。谢谢你对Nodeset的建议。你的问题用词不太合适。你基本上是要求我们推荐一些东西,或者就如何写一些东西给出我们的意见,但这会导致一个教程。相反,你应该确定你想做什么,试着写下来,然后当你遇到问题时,问一个关于这个问题的具体问题。请同时阅读“”;数据(HTML)需要在问题本身中,使用演示问题所需的最小值。链接腐烂然后断开,导致产生毫无意义的问题。您看到的问题是由于在节点集上使用了
文本。您需要迭代所有元素并分别提取它们的文本,这样才更有意义。用ruby运行系统ckmmand是否足以捕获w3m输出?@maxpleaner这是可能的,但我想知道是否可以消除这种依赖关系。@theTinMan okay下次会注意。谢谢你对Nodeset的建议。