R - How To Do Web Scraping of Key Stats From FinViz Tables Per Stock - Stack Overflow
R - How To Do Web Scraping of Key Stats From FinViz Tables Per Stock - Stack Overflow
- Stack Overflow
How to do web scraping of key stats from FinViz tables per stock?
Asked 2 years, 1 month ago Active 1 year, 6 months ago Viewed 416 times
I would like to extract the corresponding data for given stocks. I did this in the following way:
0
library(XML)
for (s in stocks) {
url <- paste0("http://finviz.com/quote.ashx?t=", s)
webpage <- readLines(url)
html <- htmlTreeParse(webpage, useInternalNodes = TRUE, asText = TRUE)
tableNodes <- getNodeSet(html, "//table")
What I would like to do is that a certain stock only appears once in the row name and that the
datanames are then displayed as column names where the columns then contain the
corresponding numbers...
r site,
By using our web-scraping rvest that
you acknowledge quantmod quandland understand our Cookie Policy, Privacy Policy, and
you have read
our Terms of Service.
https://stackoverflow.com/questions/49465312/how-to-do-web-scraping-of-key-stats-from-finviz-tables-per-stock 1/3
4/28/2020 r - How to do web scraping of key stats from FinViz tables per stock? - Stack Overflow
@mysteRious , I did see the particular post before posting, however it did not help me further with my
problem... :( – Frank Mar 24 '18 at 14:08
Oh, I finally see what it is you're trying to do. Will work on it. – mysteRious Mar 24 '18 at 18:38
@mysteRious The goal is to get it in some kind of standard wide format.. in order to make the data
analysis easier.. – Frank Mar 24 '18 at 18:44
This is not super elegant but it gets the job done. It relies on the knowledge that the data
you're interested in is in the 6th table on this page:
1
library(rvest)
url <- read_html("http://finviz.com/quote.ashx?t=AAPL")
tables <- html_nodes(url,"table")
scraped <- tables %>% html_nodes("table") %>% .[6] %>%
html_table(fill=TRUE) %>% data.frame()
> str(scraped)
'data.frame': 12 obs. of 12 variables:
$ X1 : chr "Index" "Market Cap" "Income" "Sales" ...
$ X2 : chr "DJIA S&P500" "839.87B" "53.13B" "239.18B" ...
$ X3 : chr "P/E" "Forward P/E" "PEG" "P/S" ...
$ X4 : chr "16.13" "12.50" "1.38" "3.51" ...
... the rest is truncated
This provides a results_df that looks like this, which contains all 72 variable/value pairs:
> head(results_df)
Variable Value
1 Index DJIA S&P500
2 Market Cap 839.87B
3 Income 53.13B
4 Sales 239.18B
5 Book/sh 27.42
6 Cash/sh 15.15
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
Then you can transpose it and add stock labels with
our Terms of Service.
as.data.frame(t(rbind(c("stock","AAPL"),results_df))) .
https://stackoverflow.com/questions/49465312/how-to-do-web-scraping-of-key-stats-from-finviz-tables-per-stock 2/3
4/28/2020 r - How to do web scraping of key stats from FinViz tables per stock? - Stack Overflow
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.
https://stackoverflow.com/questions/49465312/how-to-do-web-scraping-of-key-stats-from-finviz-tables-per-stock 3/3