作者tuoba (拖把)
看板AviationGame
标题[闲聊][AM] 航线查询程式原始码
时间Fri Oct 16 12:45:16 2009
这是我用来收集航线资料的程式的原始码,在这边公开
虽然这个程式还是有许多问题,收集到的资料也会有错误
但一直到目前为止,航线查询网站里面所有的资料,都是用这个程式收集而来的
这个程式是用 ruby 写的
require 'rubygems'
require 'active_record'
require 'mechanize'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "mysql user name",
:password => "mysql password",
:database => "database name"
)
class Distance < ActiveRecord::Base
end
# 以上这边是定义自己本地端的资料库
a = WWW::Mechanize.new
login_page = a.get('
http://www.facebook.com/')
login_page.form_with(:action => '
https://login.facebook.com/login.php?login_attempt=1') do |f|
f.email = 'facebook 登入用的 username'
f.pass = 'facebook 登入用的密码'
end.click_button
# 以上这边是让机器人自己登入 facebook
newroute_page = a.get('
http://apps.facebook.com/airline_manager/route.php?uID=new')
find_route_page = newroute_page.form_with(:action => 'route2.php') do |f|
end.click_button
# 以上这边是让电脑去走抓取到新增航线那一页
citylist = find_route_page.search("//select[@name='departure']/option")
# 这样的话,就可以取得目前所有的航点的列表
max_list = citylist.length-1
y = 1
citylist.each do | dcity |
citylist.slice(y..max_list).each do | acity |
#这边开始用两个回圈,把资料都扫过一次
ccity = Distance.find_by_departure_and_arrival(dcity.content, acity.content)
if ccity.nil? then
# 先查询我们自己的资料库内,有没有这笔资料,如果没有的话,就新增
goto_find_route = find_route_page.form_with(:action => 'route3.php') do |f|
f.departure = dcity.[]('value').to_i
f.arrival = acity.[]('value').to_i
end.click_button
# 这个就会查询航线,把资料拿出来
destience = ""
i = 1
goto_find_route.search('//table[@width="100%"]/tr/td/text()').each do |dest_km|
if i == 2 then
destience = dest_km.content
else
end
i = i + 1
end
# 这个就会取得网页上的距离的那个数字
newdestience = destience.gsub(/km/,'').to_i
airline = Distance.new(:departure => dcity.content , :arrival => acity.content, :distance =>newdestience )
airline.save
puts "#{dcity.content},#{acity.content},#{newdestience},"
airline = Distance.new(:departure => acity.content , :arrival => dcity.content, :distance =>newdestience )
airline.save
puts "#{acity.content},#{dcity.content},#{newdestience},"
end
end
# 这个就是把取得的航线资料,存到本地的资料库内
# 因为去回都一样,所以存成来回两笔资料
end
y = y + 1
end
# 这样就结束了
有需要的人,可以随便拿去用
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 96.226.117.78
1F:推 B77W:大推! 10/16 12:46
2F:推 supa:看无推 10/16 13:26
3F:推 Rivera01:看无推 10/16 14:15
4F:推 jsu:Ruby 的语法简洁很多,我在 php 上要自己实做一堆物件 10/16 16:53
5F:推 barryyin:这样的语法很简便!!想用C#写看看类似的好了!! 10/16 21:57