Coo*_*Day 0 r css-selectors html-parsing web-scraping rvest
我一直在使用webscraping XML::readHTMLTable
,现在我正在努力学习如何在更细微的层面上进行搜索.我的动机来自于尝试在多个日子里在网站上刮一张桌子来改变位置(例如,昨天它是页面上的第4个表格,今天它是页面上的第2个表格,等等).我将使用一个以各种体育赛事发布维加斯赔率的网站为例,我将特别试图提取NBA数据.
URL1 = "http://www.scoresandodds.com/grid_20161123.html"
URL2 = "http://www.scoresandodds.com/grid_20161125.html"
Run Code Online (Sandbox Code Playgroud)
你会注意到NBA桌子是第一张桌子URL1
,它是第二张桌子URL2
.认识到NBA是第一个表格,以下是我如何将其作为第一个网址:
library(XML)
URL1 = "http://www.scoresandodds.com/grid_20161123.html"
exTable = readHTMLTable(URL1)[[1]] %>%
# Find first blank, since NBA is the first table #
head(which(exTable[,1] == "")[1] - 1)
Run Code Online (Sandbox Code Playgroud)
然后我会从那里清理它.我知道这不是最好的方法,甚至考虑到我想要循环多天,因为需要进行所有的清洁.学习如何抓取网页表中的特定对象会更好.
我已经玩了rvest
一些,我知道我可以为Vegas线获得看起来像"td.line"的节点,但是我试图选择特定表格的节点(css = "#nba > div.sport"
或其他东西?).我不一定想要这个具体例子的答案,但学习如何做这个例子将允许我将技能应用于许多其他情况.在此先感谢您的帮助.
你在这里走得很好; 你需要一个更贴切的CSS或XPath选择器.使用rvest,如果选择器足够好,你可以使用相同的代码获取两者:
library(rvest)
URL1 = "http://www.scoresandodds.com/grid_20161123.html"
URL2 = "http://www.scoresandodds.com/grid_20161125.html"
html1 <- URL1 %>% read_html()
df1 <- html1 %>% html_node('#nba ~ div table') %>% html_table()
html2 <- URL2 %>% read_html()
df2 <- html2 %>% html_node('#nba ~ div table') %>% html_table()
str(df1)
#> 'data.frame': 65 obs. of 7 variables:
#> $ Team : chr "7:05 PM EST" "701 PHOENIX SUNS" "702 ORLANDO MAGIC" "PHO-F-T.J. Warren-? | TV: FS-Florida, DTV: 654" ...
#> $ Open : chr "7:05 PM EST" "206.5" "-4.5" "PHO-F-T.J. Warren-? | TV: FS-Florida, DTV: 654" ...
#> $ Line Movements: chr "7:05 PM EST" "207.5 / 208 / 209.5" "-4 -15 / -4.5 / -4.5 -05" "PHO-F-T.J. Warren-? | TV: FS-Florida, DTV: 654" ...
#> $ Current : chr "7:05 PM EST" "210" "-4" "PHO-F-T.J. Warren-? | TV: FS-Florida, DTV: 654" ...
#> $ Moneyline : chr "7:05 PM EST" "+155" "-175" "PHO-F-T.J. Warren-? | TV: FS-Florida, DTV: 654" ...
#> $ Halftime : chr "7:05 PM EST" "109" "-4" "PHO-F-T.J. Warren-? | TV: FS-Florida, DTV: 654" ...
#> $ Scores : chr "7:05 PM EST" "92Under 210" "87final" "PHO-F-T.J. Warren-? | TV: FS-Florida, DTV: 654" ...
str(df2)
#> 'data.frame': 75 obs. of 7 variables:
#> $ Team : chr "1:05 PM EST" "701 SAN ANTONIO SPURS" "702 BOSTON CELTICS" "TV: CSN-New England, FS-Southwest, DTV: 642, 676" ...
#> $ Open : chr "1:05 PM EST" "-2.5" "203.5" "TV: CSN-New England, FS-Southwest, DTV: 642, 676" ...
#> $ Line Movements: chr "1:05 PM EST" "-3 / -3.5 -15 / -3.5" "199 / 200 / 201" "TV: CSN-New England, FS-Southwest, DTV: 642, 676" ...
#> $ Current : chr "1:05 PM EST" "-3.5 -05" "201.5" "TV: CSN-New England, FS-Southwest, DTV: 642, 676" ...
#> $ Moneyline : chr "1:05 PM EST" "-155" "+135" "TV: CSN-New England, FS-Southwest, DTV: 642, 676" ...
#> $ Halftime : chr "1:05 PM EST" "-4.5" "106" "TV: CSN-New England, FS-Southwest, DTV: 642, 676" ...
#> $ Scores : chr "1:05 PM EST" "109Over 201.5" "103final" "TV: CSN-New England, FS-Southwest, DTV: 642, 676" ...
Run Code Online (Sandbox Code Playgroud)
在这种情况下,CSS选择器
nba
,则div
之后,再table
里面的节点.你可以在XPath中编写相同的东西,如果你愿意,可以让你使用XML包,如果你真的喜欢的话.如果你想提高你的CSS选择器技能,链接的教程?rvest::html_node
是有趣和有效的.
如果你想一次刮掉很多类似的URL,你可以将它们放在一个向量中,然后用它lapply
或更方便地迭代它purrr::map_df
.负责任地刮; Sys.sleep
在匿名函数中调用它以使其更像一个普通的站点访问者.