Last active
August 29, 2015 14:10
-
-
Save abalone0204/64a42935419890b80234 to your computer and use it in GitHub Desktop.
三商美邦分公司分佈
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 'rubygems' | |
require 'nokogiri' | |
require 'restclient' | |
module MarketShit | |
def hello | |
puts "jelko" | |
end | |
def main | |
get_page | |
get_cities_name | |
get_all_companies | |
set_default_count | |
calculate_total | |
puts @total_hash | |
end | |
def get_page | |
# 三商美邦 | |
url = "http://www.mli.com.tw/MLI_Public/AboutUsDefault.aspx?tid=Nav02&mid=52&cid=p10" | |
@page = Nokogiri::HTML(RestClient.get(url)) | |
end | |
def calculate_total | |
@city_arr.each do |city| | |
@company_arr.each do |company| | |
if company.include?(city) | |
@total_hash[city] += 1 | |
end | |
end | |
end | |
end | |
def get_cities_name | |
@city_arr = [] | |
cities=@page.css("select#ContentPlaceHolder_List_DropDownList_Company").css("option") | |
cities[2..cities.length].each do |city| | |
@city_arr.push(city.attributes["value"].value) | |
end | |
end | |
def set_default_count | |
@total_hash = {} | |
@city_arr.each do |city| | |
@total_hash[city]=0 | |
end | |
@total_hash["台北"] = 1 | |
end | |
def get_all_companies | |
@company_arr = [] | |
companies = @page.css("td:first-child") | |
companies[1..companies.size].each do |company| | |
@company_arr.push(company.children[1].children.text) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment