我想使用rvest
軟件包從Pro Football Reference網站獲取一些數據。首先,讓我們抓住從這個網址http://www.pro-football-reference.com/years/2015/games.htm
如何使用rvest()獲取表格
library("rvest")
library("dplyr")
#grab table info
url <- "http://www.pro-football-reference.com/years/2015/games.htm"
urlHtml <- url %>% read_html()
dat <- urlHtml %>% html_table(header=TRUE) %>% .[[1]] %>% as_data_frame()
在2015年玩過的所有遊戲的結果是這樣,你怎麼會做呢? :)
dat
可能會被清理一下。其中兩個變量似乎對姓名有空白。另外標題行在每週之間重複。
colnames(dat) <- c("week", "day", "date", "winner", "at", "loser",
"box", "ptsW", "ptsL", "ydsW", "toW", "ydsL", "toL")
dat2 <- dat %>% filter(!(box == ""))
head(dat2)
看起來不錯!
現在讓我們來看一個單獨的遊戲。在上面的網頁上,點擊表格第一行的「Boxscore」:9月10日比賽在新英格蘭和匹茲堡之間進行。這需要我們在這裏:http://www.pro-football-reference.com/boxscores/201509100nwe.htm
。
我想抓住每個玩家的個別對齊計數(大約在頁面中間的一半)。很確定這些將是我們的前兩行代碼:
gameUrl <- "http://www.pro-football-reference.com/boxscores/201509100nwe.htm"
gameHtml <- gameUrl %>% read_html()
但現在我無法弄清楚如何抓住我想要的特定表。我使用Selector Gadget來突出顯示Patriots snap計數表。我通過點擊幾個地方的表格來做到這一點,然後'取消'突出顯示的其他表格。我最終的路徑:
這些嘗試#home_snap_counts .right , #home_snap_counts .left, #home_snap_counts .left, #home_snap_counts .tooltip, #home_snap_counts .left
每個返回{xml_nodeset (0)}
gameHtml %>% html_nodes("#home_snap_counts .right , #home_snap_counts .left, #home_snap_counts .left, #home_snap_counts .tooltip, #home_snap_counts .left")
gameHtml %>% html_nodes("#home_snap_counts .right , #home_snap_counts .left")
gameHtml %>% html_nodes("#home_snap_counts .right")
gameHtml %>% html_nodes("#home_snap_counts")
也許讓我們嘗試使用xpath
。所有這些嘗試也將返回{xml_nodeset (0)}
gameHtml %>% html_nodes(xpath = '//*[(@id = "home_snap_counts")]//*[contains(concat(" ", @class, " "), concat(" ", "right", " "))] | //*[(@id = "home_snap_counts")]//*[contains(concat(" ", @class, " "), concat(" ", "left", " "))]//*[(@id = "home_snap_counts")]//*[contains(concat(" ", @class, " "), concat(" ", "left", " "))]//*[(@id = "home_snap_counts")]//*[contains(concat(" ", @class, " "), concat(" ", "tooltip", " "))]//*[(@id = "home_snap_counts")]//*[contains(concat(" ", @class, " "), concat(" ", "left", " "))]')
gameHtml %>% html_nodes(xpath = '//*[(@id = "home_snap_counts")]//*[contains(concat(" ", @class, " "))]')
gameHtml %>% html_nodes(xpath = '//*[(@id = "home_snap_counts")]')
我該如何抓取該表?我還會指出,當我在Google Chrome瀏覽器中查看頁面源代碼時,我想要的表格幾乎似乎已被註釋掉了。也就是說,它們以綠色打印,而不是通常的紅色/黑色/藍色配色方案。我們先抽出的比賽結果並非如此。該表格的「查看頁面源代碼」是通常的紅/黑/藍顏色方案。綠色是否代表什麼阻止了我能夠抓住這個快照表?
謝謝!
'網址< - 「http://www.pro-football-reference.com/boxscores/201509100nwe.htm#all_vis_snap_counts」 單元。計數<- url %>% read_html()%>% html_nodes(xpath ='// * [contains(concat(「」,@class,「」),concat(「」,「table_container」,「」))]') ''返回一個元素(即''{xml_nodeset(1)}'')列表,但我似乎不能將它轉換爲使用html_table(fill = TRUE)的表格'' –
''http:// www .pro-football-reference.com/boxscores/201509100nwe.htm'%>%read_html()%>%html_nodes(xpath ='// comment()')%>%html_text()%>%paste(collapse =' ')%>%read_html()%>%html_node('table#home_snap_counts')%>%html_table()%>%{setNames(。[ - 1,],paste0(names(。),。[1,] ))}%>%readr :: type_convert()' – alistaire