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.
精彩评论