Last active
December 22, 2015 03:59
-
-
Save easonhan007/6413673 to your computer and use it in GitHub Desktop.
从新浪和qq意甲页面获取国米新闻
This file contains 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 'optparse' | |
require 'watir-webdriver' | |
ENV.delete('HTTP_PROXY') | |
module Inter | |
def self.parse | |
opts = {} | |
opts[:from] = 'qq' | |
opts[:browser] = 'chrome' | |
OptionParser.new do |op| | |
op.banner = 'Usage: inter.rb [options]' | |
op.on('-n Number', '--number Number', 'number of news to show') do |n| | |
opts[:num] = n | |
end | |
op.on('-b Browser', '--browser Browser', 'what browser to visit the site') do |b| | |
opts[:browser] = b | |
end | |
op.on('-s', '--sina', 'fetch news from sina') do | |
opts[:from] = 'sina' | |
end | |
end.parse! | |
opts | |
end #parse | |
class BaseFetcher | |
def initialize url, browser | |
@list = [] | |
@url = url | |
@b = Watir::Browser.new browser | |
goto_seriea_page | |
end | |
def goto_seriea_page | |
@b.goto @url | |
end | |
def get_news_list | |
raise 'Can not call this method directly' | |
end | |
end | |
class SinaFetcher < BaseFetcher | |
def get_news_list | |
end | |
end | |
class QqFetcher < BaseFetcher | |
def get_news_list | |
@b.div(:id, 'inter').div(:class, 'hot_list').links.each { |l| @list << l.text } | |
@b.close | |
@list | |
end | |
end | |
class NewsFetcher | |
def initialize(opts) | |
@opts = opts | |
@urls = {:sina => 'www.sina.com', :qq => 'http://sports.qq.com/seriea/'} | |
@tmp_file = 'news.txt' | |
save_news_list | |
end | |
def determin_url | |
@urls[@opts[:from].to_sym] | |
end | |
def save_news_list | |
fetch_kls_name = (@opts[:from].to_s.capitalize + 'Fetcher').to_sym | |
fetcher = Inter.const_get(fetch_kls_name).new(determin_url, @opts[:browser].to_sym) | |
list = fetcher.get_news_list | |
File.open(@tmp_file, 'w') do |f| | |
f.write list.join("\n") | |
end #open | |
end | |
def show_news_list | |
list = '' | |
File.open(@tmp_file, 'r') do |f| | |
list = f.read | |
end | |
print list | |
end | |
end #NewsFetcher | |
end #Inter | |
options = Inter.parse | |
Inter::NewsFetcher.new(options).show_news_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment