【问题标题】:how to translate this hpricot code to nokogiri?如何将此 hpricot 代码翻译为 nokogiri?
【发布时间】:2011-02-08 15:45:08
【问题描述】:
 Hpricot(html).inner_text.gsub("\r"," ").gsub("\n"," ").split(" ").join(" ")

hpricot = Hpricot(html)
hpricot.search("script").remove
hpricot.search("link").remove
hpricot.search("meta").remove
hpricot.search("style").remove

http://www.savedmyday.com/2008/04/25/how-to-extract-text-from-html-using-rubyhpricot/找到它

【问题讨论】:

    标签: nokogiri hpricot


    【解决方案1】:

    Nokogiri 和 Hpricot 可以互换。 IE。 Nokogiri(html) 相当于 Hpricot(html)。不太确定我是否理解链接的文章试图达到的目的,但要:

    从 HTML 正文中提取文本,包括忽略标签和单词之间的大空格。

    这将是 Hpricot 中一种更简单的方法,并且不需要 hpricot.search("script").remove 位。 IE。首先得到身体:

    Hpricot(html).search('body').inner_text.gsub("\r"," ").gsub("\n"," ").split(" ").join(" ")
    

    在Nokogiri:

    Nokogiri(html).search('body').inner_text.gsub("\r"," ").gsub("\n"," ").split(" ").join(" ")
    

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 2019-06-21
      • 1970-01-01
      • 2011-02-22
      • 2021-09-03
      相关资源
      最近更新 更多