开发者

Nokogiri HTML parsing not working

I am trying to parse some HTML with Nokogiri, but I am not getting anything back from the css or xpath methods.

require 'rubygems'
require 'open-uri'
require 'nokogiri'

doc = Nokogiri::HTML(open("http://www.google.com"))
doc.css('div').each do |div|
   puts div.content
end
doc.xpath('//div').each do |div|
   puts div.content
end

Nothing gets printed to the screen, so css and xpath are returning empty arrays. There are at least 100 divs in Google's homepage.

doc.to_html returns:

<!DOCTYPE html>\n\n

doc.validate returns:

[#<Nokogiri::XML::SyntaxError: no root element>]

I uninstalled Nokogiri, and reinstall开发者_JAVA百科ed libxml2 and libxslt as mentioned in "Installing Nokogiri". Everything's working now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜