Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html Nokogiri';s xpath方法_Html_Ruby_Xml_Parsing_Nokogiri - Fatal编程技术网

Html Nokogiri';s xpath方法

Html Nokogiri';s xpath方法,html,ruby,xml,parsing,nokogiri,Html,Ruby,Xml,Parsing,Nokogiri,我不知道这里出了什么问题,我仔细检查了HTML源代码,有很多img标记包含该属性(class=“productImage”) 就像属性选择器不起作用一样 下面是HTML源代码的URL doc.xpath('//img') #this will get some results doc.xpath('//img[@class="productImage"]') #and this gets nothing at all doc.xpath('//div[@id="someID"]') # and

我不知道这里出了什么问题,我仔细检查了HTML源代码,有很多img标记包含该属性(class=“productImage”)

就像属性选择器不起作用一样

下面是HTML源代码的URL

doc.xpath('//img') #this will get some results
doc.xpath('//img[@class="productImage"]') #and this gets nothing at all
doc.xpath('//div[@id="someID"]') # and this won't work either

如果你有空,请帮我一个忙。像我一样解析HTML内容,看看你是否能解决这个问题。奇怪的是,如果你在该页面上使用
openURI
,你会得到一个不同于使用
curl
wget
的结果

但是,当您更改
用户代理时
实际上可能会得到您要查找的页面:

分析

http://www.amazon.cn/s/ref=nb_sb_ss_i_0_1?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&url=search-alias%3Daps&field-keywords=%E4%B8%93%E5%85%AB&x=0&y=0&sprefix=%E4%B8%93
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'pp'  

URL = 'http://www.amazon.cn/...'

def analyze_html(file)
   doc = Nokogiri.HTML(file)
   pp   doc.xpath('//img').map { |i| i[:class] }.compact.reject(&:empty?)
   puts doc.xpath('//div').map { |i| i[:class] }.grep(/productImage/).count
   puts doc.xpath('//div[@class="productImage"]//img').count
   pp   doc.xpath('//div[@class="productImage"]//img').map { |i| i[:src] }
end

puts "Attempt 1:"
analyze_html(open(URL))

puts "Attempt 2:"
analyze_html(open(URL, "User-Agent" => "Wget/1.10.2"))
输出

http://www.amazon.cn/s/ref=nb_sb_ss_i_0_1?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&url=search-alias%3Daps&field-keywords=%E4%B8%93%E5%85%AB&x=0&y=0&sprefix=%E4%B8%93
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'pp'  

URL = 'http://www.amazon.cn/...'

def analyze_html(file)
   doc = Nokogiri.HTML(file)
   pp   doc.xpath('//img').map { |i| i[:class] }.compact.reject(&:empty?)
   puts doc.xpath('//div').map { |i| i[:class] }.grep(/productImage/).count
   puts doc.xpath('//div[@class="productImage"]//img').count
   pp   doc.xpath('//div[@class="productImage"]//img').map { |i| i[:src] }
end

puts "Attempt 1:"
analyze_html(open(URL))

puts "Attempt 2:"
analyze_html(open(URL, "User-Agent" => "Wget/1.10.2"))
解决方案

http://www.amazon.cn/s/ref=nb_sb_ss_i_0_1?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&url=search-alias%3Daps&field-keywords=%E4%B8%93%E5%85%AB&x=0&y=0&sprefix=%E4%B8%93
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'pp'  

URL = 'http://www.amazon.cn/...'

def analyze_html(file)
   doc = Nokogiri.HTML(file)
   pp   doc.xpath('//img').map { |i| i[:class] }.compact.reject(&:empty?)
   puts doc.xpath('//div').map { |i| i[:class] }.grep(/productImage/).count
   puts doc.xpath('//div[@class="productImage"]//img').count
   pp   doc.xpath('//div[@class="productImage"]//img').map { |i| i[:src] }
end

puts "Attempt 1:"
analyze_html(open(URL))

puts "Attempt 2:"
analyze_html(open(URL, "User-Agent" => "Wget/1.10.2"))
  • 使用
    用户代理:Wget/1.10.2
  • 使用xpath('//div[@class=“productImage”]//img')

  • 奇怪的是,如果在该页面上使用
    openuri
    ,得到的结果与使用
    curl
    wget
    之类的方法不同

    但是,当您更改
    用户代理时
    实际上可能会得到您要查找的页面:

    分析

    http://www.amazon.cn/s/ref=nb_sb_ss_i_0_1?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&url=search-alias%3Daps&field-keywords=%E4%B8%93%E5%85%AB&x=0&y=0&sprefix=%E4%B8%93
    
    require 'rubygems'
    require 'nokogiri'
    require 'open-uri'
    require 'pp'  
    
    URL = 'http://www.amazon.cn/...'
    
    def analyze_html(file)
       doc = Nokogiri.HTML(file)
       pp   doc.xpath('//img').map { |i| i[:class] }.compact.reject(&:empty?)
       puts doc.xpath('//div').map { |i| i[:class] }.grep(/productImage/).count
       puts doc.xpath('//div[@class="productImage"]//img').count
       pp   doc.xpath('//div[@class="productImage"]//img').map { |i| i[:src] }
    end
    
    puts "Attempt 1:"
    analyze_html(open(URL))
    
    puts "Attempt 2:"
    analyze_html(open(URL, "User-Agent" => "Wget/1.10.2"))
    
    输出

    http://www.amazon.cn/s/ref=nb_sb_ss_i_0_1?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&url=search-alias%3Daps&field-keywords=%E4%B8%93%E5%85%AB&x=0&y=0&sprefix=%E4%B8%93
    
    require 'rubygems'
    require 'nokogiri'
    require 'open-uri'
    require 'pp'  
    
    URL = 'http://www.amazon.cn/...'
    
    def analyze_html(file)
       doc = Nokogiri.HTML(file)
       pp   doc.xpath('//img').map { |i| i[:class] }.compact.reject(&:empty?)
       puts doc.xpath('//div').map { |i| i[:class] }.grep(/productImage/).count
       puts doc.xpath('//div[@class="productImage"]//img').count
       pp   doc.xpath('//div[@class="productImage"]//img').map { |i| i[:src] }
    end
    
    puts "Attempt 1:"
    analyze_html(open(URL))
    
    puts "Attempt 2:"
    analyze_html(open(URL, "User-Agent" => "Wget/1.10.2"))
    
    解决方案

    http://www.amazon.cn/s/ref=nb_sb_ss_i_0_1?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&url=search-alias%3Daps&field-keywords=%E4%B8%93%E5%85%AB&x=0&y=0&sprefix=%E4%B8%93
    
    require 'rubygems'
    require 'nokogiri'
    require 'open-uri'
    require 'pp'  
    
    URL = 'http://www.amazon.cn/...'
    
    def analyze_html(file)
       doc = Nokogiri.HTML(file)
       pp   doc.xpath('//img').map { |i| i[:class] }.compact.reject(&:empty?)
       puts doc.xpath('//div').map { |i| i[:class] }.grep(/productImage/).count
       puts doc.xpath('//div[@class="productImage"]//img').count
       pp   doc.xpath('//div[@class="productImage"]//img').map { |i| i[:src] }
    end
    
    puts "Attempt 1:"
    analyze_html(open(URL))
    
    puts "Attempt 2:"
    analyze_html(open(URL, "User-Agent" => "Wget/1.10.2"))
    
  • 使用
    用户代理:Wget/1.10.2
  • 使用xpath('//div[@class=“productImage”]//img')

  • 我正在使用firebug分析DOM。我怎么能使用firebug(或Firefox)中看到的相同的DOM结构页面。我试图将用户代理设置为Mozilla/5.0(X11;Linux i686;rv:5.0)Gecko/20100101 Firefox/5.0。但它似乎不起作用,我想我得到的页面仍然与我在Firefox浏览器中看到的页面不同。@castiel-Hmm。是的,你可能会得到不同的页面。该页面充满了Javascript魔力,事实上,在我的机器上,使用
    wget-S-O页面,您的_amz_url
    似乎用302重定向到此页面:。我怀疑你真的需要仔细分析整个页面和其中的Javascript来完全了解发生了什么。是的,我认为你是对的。不管怎样,你知道有什么宝石或方法可以为我美化HTML源代码吗?我很愚蠢,我保存了OpenURI获取的页面,没有指定用户代理,并使用Firebug分析了我保存的文件。哦,天哪,这是一个完全不同的页面,具有不同的DOM结构,充满了表td,tr。这就是我的原始代码无法工作的原因,因为没有div(我想要的div)不管怎样。现在我根据我得到的正确页面重写了xPath表达式。达拉,现在一切都正常工作了。谢谢你的启发,@CasperI正在使用firebug分析DOM。我如何才能使用firebug(或Firefox)中看到的相同DOM结构页面。我尝试将用户代理设置为Mozilla/5.0(X11;Linux i686;rv:5.0)Gecko/20100101 Firefox/5.0。但它似乎不起作用,我想我得到的页面仍然与我在Firefox浏览器中看到的页面不同。@castiel-Hmm。是的,你可能会得到一个不同的页面。该页面充满了Javascript魔法,事实上,在我的机器上,在
    wget-S-O页面上,你的_amz_url
    似乎会用302重定向到另一个页面此页面:。我想你真的需要仔细分析整个页面和其中的Javascript才能完全了解发生了什么。是的,我认为你是对的。不管怎样,你知道有什么可以美化HTML源代码的gem或方法吗?我很笨,我保存了通过open uri获取的页面,但没有指定用户代理并使用Fire分析我保存的文件时出现错误。哦,天哪,这是一个完全不同的页面,具有不同的DOM结构,充满了table,td,tr。这就是我的原始代码无法工作的原因,因为没有div(我想要的div)不管怎样。现在我根据我得到的正确页面重写了xPath表达式。达拉,现在一切正常。谢谢你的启发,@Casper