Last active
September 5, 2015 00:45
-
-
Save krongk/410145 to your computer and use it in GitHub Desktop.
A Nokogiri Demo test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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