2017-08-18 52 views
0

我想從這個 網站獲得的表沒有定義的表rvest: http://www.oddsportal.com/american-football/usa/nfl-2012-2013/results/的Web刮從

其實我想要得到的表在頁面的中間。

我嘗試過不同的方式,但徒勞無功。

library("rvest") 
library(dplyr) 

url1 <- "http://www.oddsportal.com/american-football/usa/nfl-2012-2013/results/" 
table <- url1 %>% 
    read_html() %>% 
    html_nodes(xpath='//*[@id="tournamentTable"]') %>% 
    html_table(fill = T) 

這是行不通的,因爲我認爲該表沒有定義爲表。

我也試圖通過使用單獨搶行:

df <- mps1 %>% 
    html_nodes(css = "tr.odd.deactivate,tr.center.nob-border") 

,但它獲得什麼。

任何想法我該怎麼做?

感謝

回答

2

的人試圖從該網站湊根據以往的問題,可能是動態生成此表。據我所知,處理這樣的頁面的唯一方法是使用RSelenium - 這基本上可以自動瀏覽器。

大量的試驗和錯誤後,下面的代碼似乎工作(使用Chrome在Windows 10)...

library(RSelenium) 
library(rvest) 
library(dplyr) 

url <- "http://www.oddsportal.com/american-football/usa/nfl-2012-2013/results/" 
rD <- rsDriver(port=4444L,browser="chrome") 
remDr <- rD$client 

remDr$navigate(url) 

page <- remDr$getPageSource() 
remDr$close() #you can leave open if you are doing several of these: close at the end 

table <- page[[1]] %>% 
    read_html() %>% 
    html_nodes(xpath='//table[@id="tournamentTable"]') %>% #specify table as there is a div with same id 
    html_table(fill = T) 

table <- table[[1]] 

head(table) 

    American Football» USA»NFL 2012/2013 American Football» USA»NFL 2012/2013 American Football» USA»NFL 2012/2013 American Football» USA»NFL 2012/2013 American Football» USA»NFL 2012/2013 American Football» USA»NFL 2012/2013 
1    03 Feb 2013 - Play Offs    03 Feb 2013 - Play Offs    03 Feb 2013 - Play Offs    03 Feb 2013 - Play Offs         1.00         2.00 
2                                               NA         NA 
3        23:30 San Francisco 49ers - Baltimore Ravens San Francisco 49ers - Baltimore Ravens        31:34         1.49         2.71 
4    28 Jan 2013 - All Stars    28 Jan 2013 - All Stars    28 Jan 2013 - All Stars    28 Jan 2013 - All Stars         1.00         2.00 
5                                               NA         NA 
6        00:00        NFC - AFC        NFC - AFC        62:35         2.03         1.83 
    American Football» USA»NFL 2012/2013 
1         B's 
2          
3         9 
4         B's 
5          
6         9 

勝算出來十進制數,很遺憾,但希望你能與此一起工作。

+0

非常感謝!我以爲RSelenium是用來在頁面中導航的。 (點擊按鈕等) –