ruby-on-rails - 如何提取 HTML 页面中的搜索元名称以使用 Nokogiri 提取内容

标签 ruby-on-rails ruby nokogiri asp.net-web-api httparty

我正在试用 Nokogiri 和 HTTparty 来获取在我的 Rails 模型中生成的内容验证 token 。此元标记有一个 UUID,用户将该标记粘贴到他们的 index.html 文件中以进行验证:

领域模型:

class Domain
  include Mongoid::Document

  field :name, type: String
  field :url, type: String
  field :active, type: Boolean, default: false
  field :verification_token, type: String

  belongs_to :user

  before_create :generate_verification_token

  protected

  def generate_verification_token
    self.verification_token = SecureRandom.hex(30)
  end
end

域 View :

<h1><%= @domain.name %></h1>

<p><%= @domain.url %></p>

<% if !@domain.active? %>
<div class="alert alert-danger">
  <strong>Oops!</strong> Your domain is not verified!.
</div>
<div class="lead">
  Please add the following meta tag to your domain root url page
  <pre><%= "<meta name='trackmetrics_verification' content='#{@domain.verification_token}'/>" %></pre>
</div>
<% end %>
<%= link_to "Edit", edit_domain_path(@domain), class: 'btn btn-success' %>
<%= link_to "New Domain", new_domain_path(@domain), class: 'btn btn-success' %>
<p><%= link_to "Back to List", domains_path %></p>

本质上,用户将包括

<meta name='trackmetrics_verification' content='21bd47859ea549244e66a4582e99068fe0bc2063d6276a4426c2c3bb2e15'/>

在他们的根 url 页面 (index.html) 中,我想要一个验证 Controller 来检查用户是否真的在该页面中包含了元标记。如果他们这样做了,他们的域将被标记为 True。

我的验证 Controller :

class VerificationController < ApplicationController

  require 'nokogiri'
  require 'httparty'

  def index
    @domain = Domain.find(params[:Domain_id]) 
    @doc = Nokogiri::HTML(HTTParty.get(@domain.url))
  end

end

我的问题是我将如何捕获它

<meta name='trackmetrics_verification' content='21bd47859ea549244e66a4582e99068fe0bc2063d6276a4426c2c3bb2e15'/>

并使用 Nokogiri 查找 trackmetrics_verification 元名称?

会不会是这样的

def index
        @domain = Domain.find(params[:Domain_id]) 
        @doc = Nokogiri::HTML(HTTParty.get(@domain.url))
        if @doc.xpath("//meta[@name ="trackmetrics_verification"]")
           @domain.active = true
           flash[:notice] = "Your domain has been verified!"
       else
           flash[:error] = "Incorrect verification"
       end
end

最佳答案

您使用的 XPath,//meta[@name ="trackmetrics_verification"],将返回任何具有 name< 的 meta 元素节点 trackmetrics_verification 的属性(可能应该只有一个这样的节点)。您需要此节点的 content 属性。获取它的一种方法是扩展查询以指定属性:

//meta[@name ="trackmetrics_verification"]/@content

与 Nokogiri,使用 at_xpath由于您只期望一个匹配节点,因此您可以使用 text 方法获取属性节点的值:

@doc.at_xpath('//meta[@name ="trackmetrics_verification"]/@content').text

Nokogiri 的另一种选择是选择 meta 节点并使用 [] method获取属性的值:

@doc.at_xpath('//meta[@name ="trackmetrics_verification"])['content']

关于ruby-on-rails - 如何提取 HTML 页面中的搜索元名称以使用 Nokogiri 提取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24588962/

相关文章:

ruby-on-rails - 如何启用 :tsearch dictionary for pg_search multi-search?

ruby-on-rails - 不可逆迁移 - 警告并确认而不是中止?

ruby - 将散列与 ruby​​ 中字符串的键/值合并

ruby-on-rails - Rails 项目 : NoMethodError: undefined method `identifier' for String

html - <A> 中的换行符不起作用

ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法?

ruby - 使用 XPath 按多个值过滤

ruby-on-rails - 简单的Nokogiri不工作: undefined method call for "Nokogiri"

ruby-on-rails - Nginx 乘客集成模式总是需要 Passengerfile.json 文件?