Vis Bandla
Vis Bandla

Reputation: 39

Extract table using rvest from website

I am trying to use rvest to extract data from table. Following is the code I am using

mcurl<-read_html("http://www.moneycontrol.com/financials/tataconsultancyservices/balance-sheetVI/TCS#TCS")

Using the following code I am only able to get the header instead of the content of the entire table.

html_table(html_nodes(mcurl, "table.table4")[2],header=FALSE,fill=TRUE)
[[1]]
                                                  X1                                                 X2
1 Balance Sheet of Tata Consultancy Services ------------------- in Rs. Cr. -------------------


html_table(html_nodes(mcurl, "table")[4],header=FALSE,fill=TRUE)
[[1]]
                                          X1                                                 X2
1 Balance Sheet of Tata Consultancy Services ------------------- in Rs. Cr. -------------------

Upvotes: 1

Views: 723

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18425

I'm not sure why rvest struggles with this one, but you can use readHTMLTable from the XML package to do the same thing...

library(XML)
tables <- readHTMLTable("http://www.moneycontrol.com/financials/tataconsultancyservices/balance-sheetVI/TCS#TCS")

head(tables[[5]],10)
                                 Mar 17    Mar 16    Mar 15    Mar 14    Mar 13
1                                  <NA>      <NA>      <NA>      <NA>      <NA>
2                               12 mths   12 mths   12 mths   12 mths   12 mths
3                                  <NA>      <NA>      <NA>      <NA>      <NA>
4    EQUITIES AND LIABILITIES                                              <NA>
5         SHAREHOLDER'S FUNDS                                              <NA>
6        Equity Share Capital    197.00    197.04    195.87    195.87    195.72
7    Preference Share Capital      0.00      0.00      0.00      0.00    100.00
8         Total Share Capital    197.00    197.04    195.87    195.87    295.72
9        Reserves and Surplus 77,825.00 58,669.82 45,220.57 43,856.01 32,266.53
10 Total Reserves and Surplus 77,825.00 58,669.82 45,220.57 43,856.01 32,266.53

Upvotes: 1

Related Questions