开发者

Nokogiri HTML parsing not working

开发者 https://www.devze.com 2023-04-03 00:53 出处:网络
I am trying to parse some HTML with Nokogiri, but I am not getting anything back from the css or xpath methods.

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

精彩评论

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

关注公众号