2017-08-12 172 views
0

我正在嘗試使用rvest從表格中提取數據。以下是我正在使用的代碼從網站使用rvest提取表格

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

使用以下代碼我只能夠獲取標題而不是整個表的內容。

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. ------------------- 

回答

1

我不知道爲什麼rvest鬥爭這一個,但你可以使用readHTMLTableXML包做同樣的事情...

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 
+0

謝謝你提的關於readHTMLTable。我試圖實現的不僅僅是從此頁面上顯示的表中提取數據。表格「過去幾年」右側有一個鏈接。點擊這將導致另一組數據,但我想要使用html_session提取相同的URL,因此想使用rvest提取數據,但如果使用url使用readHTMLTable將面臨約束。 –