class Strock

Public Instance Methods

bot() click to toggle source
# File lib/strock_bot.rb, line 10
def bot
    str = ""
    alphabets = "A".."Z"
    alphabets.to_a.each do |i|
        url = "https://www.set.or.th/set/commonslookup.do?language=th&country=TH&prefix=#{i}"
        source = Net::HTTP.get(URI.parse(url))
        document = Nokogiri::HTML.parse(source)
        td_arr = document.css("td")
        (0..td_arr.length-1).each do |j|
            if (j+1)%3 == 0
                link = "https://www.set.or.th/set/companyhighlight.do?symbol=#{td_arr[j-2].text}"
                src = Net::HTTP.get(URI.parse(link))
                doc = Nokogiri::HTML.parse(src)
                td = doc.css("td").to_a
                (3..td.length-1).each do |i|
                    if td[i].text == "หนี้สินรวม"
                        str += td[i-2].text
                        break;
                    end
                end
                str += "\n"
            else
                str += td_arr[j].text + " : "
            end
        end
    end
    puts str
end