Skip to content

Instantly share code, notes, and snippets.

@iannono
Created June 28, 2013 03:47
Show Gist options
  • Save iannono/5882340 to your computer and use it in GitHub Desktop.
Save iannono/5882340 to your computer and use it in GitHub Desktop.
抓取天气的小脚本
#!/usr/bin/env ruby
#encoding:utf-8
require 'csv'
require 'open-uri'
require 'nokogiri'
require 'date'
def generate_url(date)
"http://history.tianqizx.cn/his_1582_" + date.strftime('%Y/%-m/%-d') + ".aspx"
end
start_date = Date.new(2013, 4, 28)
end_date = Date.new(2013,5, 31)
day_num = end_date - start_date
CSV.open("file1.csv", 'wb') do |csv|
day_num.to_i.times do |i|
weather_url = generate_url(start_date + i)
doc = Nokogiri::HTML(open(weather_url))
contents = doc.css('#tr_1>td:first').inner_html().split('<br>')
csv << contents.push(start_date + i)
end
end
puts "OK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment