Skip to content

Instantly share code, notes, and snippets.

@krongk
Last active September 5, 2015 00:45
Show Gist options
  • Save krongk/410145 to your computer and use it in GitHub Desktop.
Save krongk/410145 to your computer and use it in GitHub Desktop.
A Nokogiri Demo test
require 'nokogiri'
require 'open-uri'
require 'iconv'
# define the html url
url="http://cd.58.com/wangzhanmeigong/"
#get the nokogiri document
doc = Nokogiri::HTML(open(url))
#new a file for save
file = File.new("c:/a.html","w")
file.write("\<html\>\<head\>\<meta http-equiv=\"content-type\" content=\"text/html;charset=UTF-8\" \/>\</head\>\<body\>")
#get the default encoding
default_encode=doc.meta_encoding
#get the info list into a array
arr =[]
doc.css("table").each do |line|
line.content
arr << line
end
#convert the default encoding to utf-8
c = Iconv.iconv("utf-8//IGNORE",default_encode,arr.to_s)
#write the info to the file
file.write(c)
file.write("\</body\>\</html\>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment