【问题标题】:Rails 4 - Fetch html Tag Value From Nokogiri::HTML::Document Using Nokogiri GemRails 4 - 使用 Nokogiri Gem 从 Nokogiri::HTML::Document 获取 html 标记值
【发布时间】:2014-08-13 06:42:28
【问题描述】:

如何将 Nokogiri::HTML::Document 内容转换为纯 html 格式。我使用 Nokogiri gem 得到了以下内容

#<Nokogiri::HTML::Document:0x63a1d2a name="document" children=[#<Nokogiri::XML::DTD:0x63a1b0e name="html">, #<Nokogiri::XML::Element:0x63a1910 name="html" children=[#<Nokogiri::XML::Element:0x63a1654 name="body" children=[#<Nokogiri::XML::Element:0x63a14ba name="div" attributes=[#<Nokogiri::XML::Attr:0x63a1442 name="dir" value="ltr">] children=[#<Nokogiri::XML::Text:0x63a0f7e "This is testing comment">, #<Nokogiri::XML::Element:0x63a0eca name="br">]>, #<Nokogiri::XML::Element:0x63a0ac4 name="div" attributes=[#<Nokogiri::XML::Attr:0x63a0a4c name="class" value="gmail_extra">] children=[#<Nokogiri::XML::Element:0x63a0524 name="br">, #<Nokogiri::XML::Element:0x63a02c2 name="br">, #<Nokogiri::XML::Element:0x63a004c name="div" attributes=[#<Nokogiri::XML::Attr:0x63a400c name="class" value="gmail_quote">] children=[#<Nokogiri::XML::Text:0x63a5dbc "On Wed, Aug 13, 2014 at 11:34 AM,  ">, #<Nokogiri::XML::Element:0x63a5d44 name="span" attributes=[#<Nokogiri::XML::Attr:0x63a5ca4 name="dir" value="ltr">] children=[#<Nokogiri::XML::Text:0x63a5966 "<">, #<Nokogiri::XML::Element:0x63a58e4 name="a" attributes=[#<Nokogiri::XML::Attr:0x63a586c name="href" value="mailto:demo4582@gmail.com">, #<Nokogiri::XML::Attr:0x63a584e name="target" value="_blank">] children=[#<Nokogiri::XML::Text:0x63a539e "demo4582@gmail.com">]>, #<Nokogiri::XML::Text:0x63a522c ">">]>, #<Nokogiri::XML::Text:0x63a5074 " wrote:">, #<Nokogiri::XML::Element:0x63a4fc0 name="br">, #<Nokogiri::XML::Text:0x63a4dc2 "\n">, #<Nokogiri::XML::Element:0x63a4bec name="blockquote" attributes=[#<Nokogiri::XML::Attr:0x63a4b74 name="class" value="gmail_quote">, #<Nokogiri::XML::Attr:0x63a4b6a name="style" value="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">] children=[#<Nokogiri::XML::Element:0x63a450c name="u">, #<Nokogiri::XML::Text:0x63a42be "\n\n  \n    \n  \n  ">, #<Nokogiri::XML::Element:0x63a4174 name="div" children=[#<Nokogiri::XML::Text:0x63aa98e "\n    ">, #<Nokogiri::XML::Element:0x63aadb2 name="div" children=[#<Nokogiri::XML::Text:0x63abe60 "\n      ">, #<Nokogiri::XML::Element:0x63abde8 name="h3" children=[#<Nokogiri::XML::Text:0x63abbf4 "Dear Jignesh">]>, #<Nokogiri::XML::Text:0x63aba50 "\n      ">, #<Nokogiri::XML::Element:0x63ab9a6 name="input" attributes=[#<Nokogiri::XML::Attr:0x63ab96a name="name" value="notification_tyle">, #<Nokogiri::XML::Attr:0x63ab960 name="value" value="notes">, #<Nokogiri::XML::Attr:0x63ab956 name="type" value="hidden">]>, #<Nokogiri::XML::Text:0x63ab370 "\n      ">, #<Nokogiri::XML::Element:0x63ab2b2 name="p" children=[#<Nokogiri::XML::Text:0x63ab0b4 "\n        Following are the contents of the note you have been tagged in:\n      ">]>, #<Nokogiri::XML::Text:0x63aae70 "\n      ">, #<Nokogiri::XML::Element:0x63aadda name="p" children=[#<Nokogiri::XML::Text:0x63aac2c "\n        dfdfd dffffffffff dfffffffffffffff\n      ">]>, #<Nokogiri::XML::Text:0x63aaaec "\n      ">, #<Nokogiri::XML::Element:0x63aaa2e name="br">, #<Nokogiri::XML::Text:0x63aa83a "\n      ">, #<Nokogiri::XML::Element:0x63aa722 name="p" children=[#<Nokogiri::XML::Text:0x63aa4de "\n        Kind Regards\n      ">]>, #<Nokogiri::XML::Text:0x63aa2d6 "\n      ">, #<Nokogiri::XML::Element:0x63aa218 name="p" children=[#<Nokogiri::XML::Text:0x63aa0b0 "\n        The DRIVE Programme Team\n      ">]>, #<Nokogiri::XML::Text:0x63ae7f0 "\n    ">]>, #<Nokogiri::XML::Text:0x63affb0 "\n  ">]>, #<Nokogiri::XML::Text:0x63afe8e "\n\n">]>]>, #<Nokogiri::XML::Element:0x63afd1c name="br">]>, #<Nokogiri::XML::Text:0x63afb78 "\n">]>]>]>

控制器/participant_controller.rb

 def callback
    html = params[:html].html_safe
    doc = Nokogiri::HTML(html)
    logger.debug "--------Inspect---------#{doc.inspect}"
    params[:participant_attachments] = {}
    params[:participant_attachments][:content] = params[:reply_plain]
    note = Note.new(activity_params)
    note.save
  end

如何从这些参数中找到隐藏字段值?我想获取“notification_type”的隐藏输入标签值。

提前致谢。

【问题讨论】:

  • 要小心,因为输入的名称是notification_tyle,而不是notification_type

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2 nokogiri


【解决方案1】:

您可以使用 css 选择器。

doc.css("input[name=notification_type]")['value']

【讨论】:

  • -TypeError(没有将字符串隐式转换为整数):app/controllers/participant/participant_attachments_controller.rb:47:in []' app/controllers/participant/participant_attachments_controller.rb:47:in callback'
  • 使用 doc.css("input[name=notification_type]") 获得了整个标签,但我无法使用 doc.css("input[name=notification_type]")[' value'].任何解决方案??
  • 这是获取值的方式。否则你可以试试 doc.css("input[name=notification_type]").content
猜你喜欢
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 2014-06-21
相关资源
最近更新 更多