ruby

领取游戏新手卡来http://5433.com

导航

Nokogiri采集的一个小例子

1 require 'nokogiri'
2 require 'open-uri'
3 require "iconv"
4
5 task :dreamtype => :environment do
6 # doc = Nokogiri::HTML(File.open("a.html"))
7 # doc.css("table.txt>tr>td:nth-child(1)").each do |link|
8 # Dream.create(:url => "a",:name => link.content)
9 # end
10 # da = Iconv.iconv("UTF-8","GB2312",Nokogiri::HTML(open(url)))
11 ic = Iconv.new("utf-8//IGNORE","gb2312//IGNORE")
12 doc = Nokogiri::HTML(File.open("a.html"))
13 doc.css("table.txt>tr:nth-child(8)>td:nth-child(2)>a").each do |k|
14 url = "http://www.tongcha.com/"+k[:href]
15 da = Nokogiri::HTML(open(url))
16 da.css("table.txt>tr:nth-child(1)>td").each do |c|
17 #puts c.content
18 DayDream.create(:dream_id => 8,:name => k.content,:content => c.content)
19 end
20 puts url
21 end
22 end

 

posted on 2010-03-26 12:56  最初的模样  阅读(1263)  评论(0编辑  收藏  举报