Skip to content

Instantly share code, notes, and snippets.

@abalone0204
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abalone0204/64a42935419890b80234 to your computer and use it in GitHub Desktop.
Save abalone0204/64a42935419890b80234 to your computer and use it in GitHub Desktop.
三商美邦分公司分佈
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