2016-02-29 19 views
5

使用以下documentation我一直在試圖從marketwatch.com如何用rvest和xpath擦一張桌子?

這裏颳了一系列的表格,是波紋管的代碼所表示的一個:

enter image description here

鏈接和XPath已經包含在代碼:

url <- "http://www.marketwatch.com/investing/stock/IRS/profile" 
valuation <- url %>% 
    html() %>% 
    html_nodes(xpath='//*[@id="maincontent"]/div[2]/div[1]') %>% 
    html_table() 
valuation <- valuation[[1]] 

我得到以下錯誤:

Warning message: 
'html' is deprecated. 
Use 'read_html' instead. 
See help("Deprecated") 

在此先感謝。

+3

刪除'html()'並替換爲'read_html()' – cory

+0

這不是一個錯誤,這是一個警告。您的代碼仍然會以該警告運行。 – SymbolixAU

+0

謝謝。固定。 –

回答

3

該網站不使用html表格,所以html_table()找不到任何東西。它實際上使用divcolumndata lastcolumn

所以,你可以這樣做

url <- "http://www.marketwatch.com/investing/stock/IRS/profile" 
valuation_col <- url %>% 
    read_html() %>% 
    html_nodes(xpath='//*[@class="column"]') 

valuation_data <- url %>% 
    read_html() %>% 
    html_nodes(xpath='//*[@class="data lastcolumn"]') 

甚至

url %>% 
    read_html() %>% 
    html_nodes(xpath='//*[@class="section"]') 

爲了讓你大部分的方式存在。

請同時閱讀他們的terms of use - 特別是3.4。