class Scraper

Instead of printing Delta's information. Your script looks up at www.set.or.th/set/commonslookup.do and loop through all stock data and print their assets in stead.

Attributes

parse_page[RW]

Public Class Methods

execute() click to toggle source
# File lib/lab3.rb, line 87
def self.execute
    scraper = Scraper.new
    header_link = scraper.get_header_link[2..-1]
    company_link = scraper.get_company_link(header_link)
    names = scraper.get_names(company_link)
    assets = scraper.get_assets(company_link)

    arr = names.zip(assets)
    arr.each{|element| puts "#{element[0]} : #{element[1]}"}
end
new() click to toggle source
# File lib/lab3.rb, line 44
def initialize
    url = HTTParty.get('https://www.set.or.th/set/commonslookup.do')
    @parse_page ||= Nokogiri::HTML(url.body)
end

Public Instance Methods

get_assets(company_link) click to toggle source
# File lib/lab3.rb, line 76
def get_assets(company_link)
    assets = []
    company_link.each do |link|
        link = "https://www.set.or.th/set/companyhighlight" + link[19...42] + "5&language=th&country=TH"
        url = HTTParty.get(link)
        third_parse_page ||= Nokogiri::HTML(url.body)
        assets.push(third_parse_page.css("tr")[2].css("td")[-2].text)
    end
    return assets
end
get_names(company_link) click to toggle source
# File lib/lab3.rb, line 65
def get_names(company_link)
    names = []
    company_link.each do |link|
        link = "https://www.set.or.th/" + link
        url = HTTParty.get(link)
        third_parse_page ||= Nokogiri::HTML(url.body)
        names.push(third_parse_page.css("h3").text)
    end
    return names
end