2017-09-04 66 views
0

我正在學習如何在R中使用httrXML從網站上刮取信息。我只是爲了只有幾張表的網站就可以工作,但無法計算出它用於有多個表格的網站。利用親足球參考以下頁面爲例:https://www.pro-football-reference.com/boxscores/201609110atl.htmR:在URL中搜刮多個表格

# To get just the boxscore by quarter, which is the first table: 
URL = "https://www.pro-football-reference.com/boxscores/201609080den.htm" 
URL = GET(URL) 
SnapTable = readHTMLTable(rawToChar(URL$content), stringAsFactors=F)[[1]] 

# Return the number of tables: 
AllTables = readHTMLTable(rawToChar(URL$content), stringAsFactors=F) 
length(AllTables) 
[1] 2 

所以我能刮信息,但由於某些原因,我只能捕捉前兩名錶了20+的頁。爲了練習,我試圖獲得「Starters」表和「官員」表。

我無法將其他表格設置爲網站設置或不正確的代碼嗎?

回答

0

如果涉及到R的網頁抓取,請密切使用軟件包rvest。

雖然設法得到html很好 - rvest使用了css選擇器 - SelectorGadget幫助找到特定表格的樣式,希望它是唯一的。因此,您可以精確提取您正在查找的表格而不是巧合

爲了讓您開始 - 閱讀有關rvest的小插曲以獲取更多詳細信息。

#install.packages("rvest") 
library(rvest) 
library(magrittr) 

# Store web url 
fb_url = "https://www.pro-football-reference.com/boxscores/201609080den.htm" 

linescore = fb_url %>% 
    read_html() %>% 
    html_node(xpath = '//*[@id="content"]/div[3]/table') %>% 
    html_table() 

希望這有助於。