celestialgod

EPS vs Price

Mar 18th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.23 KB | None | 0 0
  1. library(httr)
  2. library(pipeR)
  3. library(xml2)
  4. library(rvest)
  5. library(ggplot2)
  6. library(plotly)
  7.  
  8. tableNode <- GET("http://pchome.megatime.com.tw/rank/sto2/ock00.html",
  9.     add_headers(Connection = "Keep-Alive", Accept = "text/html, application/xhtml+xml, */*",
  10.                 "Accept-Language" = "en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3",
  11.                 "User-Agent" = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko",
  12.                 referer = "http://pchome.megatime.com.tw/rank/"),
  13.     set_cookies(stock_popup_personalnews = 1, website_viewmode="normal",
  14.                 stock_config = "YTozOntzOjc6IlN0b2NrSWQiO047czozOiJ0YWciO2k6MztzOjQ6InR5cGUiO2k6Mjt9")) %>>%
  15.   content %>>% xml_find_all("//div[@id='bttb']") %>>% xml_find_all("table")
  16.  
  17. if (Sys.info()[[1]] == "windows") {
  18.   original_locale <- Sys.getlocale("LC_COLLATE")
  19.   Sys.setlocale("LC_ALL", 'C')
  20.   resTbl <- html_table(tableNode)[[1]]
  21.   Sys.setlocale(locale = original_locale)
  22. } else {
  23.   resTbl <- html_table(tableNode)[[1]]
  24. }
  25.  
  26. names(resTbl) <- c("rank", "stock", "eps_2016Q4", "eps_2015Q4", "price", paste0("V", 1:5))
  27.  
  28. p <- ggplot(resTbl, aes(price, eps_2016Q4, stock = stock)) + geom_point()
  29. ggplotly(p, tooltip = c("x", "y", "stock"))
Advertisement
Add Comment
Please, Sign In to add comment