使用ruby nokogiri从搜狐旅游新闻频道抓点东东做成分类的训练预料:
#!/usr/bin/ruby
#
# Author: fuliang http://fuliang.javaeye.com
#
require 'open-uri'
require 'nokogiri'
link_xpath = "/html/body/div/div[4]/div[1]/div/ul/li/a"
content_xpath = '//*[@id="contentText"]'
10.upto(53) do |index|
url = "http://travel.sohu.com/lvyouxinwen_#{index}.shtml"
STDERR.puts "processing #{url}..."
Nokogiri::HTML(open(url)).xpath(link_xpath).each do |item|
link,title = item.attributes["href"], item.content
next unless link && title
title = title.gsub(/\s+|[<>]/,"")
contents = Nokogiri::HTML(open(link)).xpath(content_xpath)
next unless contents && !contents.empty?
content = contents[0].content.gsub(/\s+|[<>]/m,"")
puts "<TitleWords: %s><BodyWords: %s><category:TravelNews>" % [ title, content ]
end
end