R和RSelenium刮javascript表

时间:2017-02-05 21:45:43

标签: r selenium

我对R很新,所以请原谅我完全天真。我正在尝试使用R和Selenium从Fangraphs中剔除棒球数据。不幸的是,我想使用的图表是javascript,并且图表的备份数据不公开(或者我无法通过检查找到它)。我试图使用Selenium来抓取这些数据,但我不熟悉代码。

我正在尝试从此图表中提取数据,并使用循环迭代地拉出不同的变化日期。

http://www.fangraphs.com/leaderssplits.aspx?splitArr=&strgroup=season&statgroup=2&startDate=2016-3-1&endDate=2016-11-1&filter=&position=B&statType=player&autoPt=true&sort=16,1&pg=0

如果我能提供更多信息,请告诉我。我衷心感谢任何和所有的帮助。谢谢你的阅读!

1 个答案:

答案 0 :(得分:1)

虽然我不愿意参与体育运动的努力,但是当有一个非常好的JSON XHR响应时,不需要Selenium:

library(httr)
library(jsonlite)
library(tidyverse)

options(width=120)

res <- POST("http://www.fangraphs.com/splitstool.aspx/getsplitleaders",
            content_type_json(),
            body = '{"strPlayerId": "all", "strSplitArr": "[]", "strGroup": "season", "strPosition": "B", "strType": "2", "strStartDate": "2016-3-1", "strEndDate": "2016-11-1", "strSplitTeams": "false", "dctFilters": [], "strStatType": "player", "strAutoPt": "true"}')

ret <- fromJSON(content(res, as="text"))

df <- setNames(as_data_frame(ret$d$v), ret$d$k)

glimpse(df)
## Observations: 362
## Variables: 17
## $ Season <chr> "2016", "2016", "2016", "2016", "2016", "2016", "2016", "2016", "2016", "2016", "2016", "2016", "201...
## $ Name   <chr> "<a href=\"http://www.fangraphs.com/statss.aspx?playerid=393\">Victor Martinez</a>", "<a href=\"http...
## $ Team   <chr> "DET", "CLE", "2 Tms", "TEX", "BOS", "ATL", "CHW", "CIN", "MIA", "DET", "LAA", "MIA", "NYY", "NYY", ...
## $ PA     <chr> "610", "259", "593", "640", "626", "259", "666", "584", "365", "194", "650", "264", "243", "438", "6...
## $ BB%    <chr> "0.081967213", "0.057915057", "0.059021922", "0.075", "0.127795527", "0.023166023", "0.096096096", "...
## $ K%     <chr> "0.147540983", "0.189189189", "0.170320404", "0.103125", "0.137380191", "0.111969111", "0.244744744"...
## $ BB/K   <chr> "0.555555555", "0.306122448", "0.346534653", "0.727272727", "0.930232558", "0.206896551", "0.3926380...
## $ AVG    <chr> "0.289330922", "0.205882352", "0.295289855", "0.300171526", "0.314711359", "0.218623481", "0.2254237...
## $ OBP    <chr> "0.350819672", "0.258687258", "0.337268128", "0.3578125", "0.400958466", "0.243243243", "0.302255639...
## $ SLG    <chr> "0.475587703", "0.331932773", "0.512681159", "0.521440823", "0.620111731", "0.303643724", "0.4644067...
## $ OPS    <chr> "0.826407375", "0.590620031", "0.849949287", "0.879253323", "1.021070197", "0.546886967", "0.7666624...
## $ ISO    <chr> "0.186256781", "0.126050421", "0.217391304", "0.221269297", "0.305400372", "0.085020243", "0.2389830...
## $ BABIP  <chr> "0.302961275", "0.227027027", "0.31455399", "0.293032786", "0.311904761", "0.237442922", "0.23604060...
## $ wRC    <chr> "88.2665363979459", "17.2829323978341", "89.4981070690418", "103.469989324776", "125.949712448356", ...
## $ wRAA   <chr> "16.4085380268217", "-13.2272669105613", "19.6427086525227", "28.0779910337601", "52.2069141199561",...
## $ wOBA   <chr> "0.350698787013558", "0.256208411251852", "0.358216146661794", "0.371267644106778", "0.4191683159132...
## $ wRC+   <chr> "119.598350421384", "54.0784826585152", "123.808772024583", "130.393352062375", "162.529328454689", ...

df
## # A tibble: 362 × 17
##    Season                                                                             Name  Team    PA       `BB%`
##     <chr>                                                                            <chr> <chr> <chr>       <chr>
## 1    2016  <a href="http://www.fangraphs.com/statss.aspx?playerid=393">Victor Martinez</a>   DET   610 0.081967213
## 2    2016       <a href="http://www.fangraphs.com/statss.aspx?playerid=454">Juan Uribe</a>   CLE   259 0.057915057
## 3    2016   <a href="http://www.fangraphs.com/statss.aspx?playerid=589">Carlos Beltran</a> 2 Tms   593 0.059021922
## 4    2016    <a href="http://www.fangraphs.com/statss.aspx?playerid=639">Adrian Beltre</a>   TEX   640       0.075
## 5    2016      <a href="http://www.fangraphs.com/statss.aspx?playerid=745">David Ortiz</a>   BOS   626 0.127795527
## 6    2016  <a href="http://www.fangraphs.com/statss.aspx?playerid=746">A.J. Pierzynski</a>   ATL   259 0.023166023
## 7    2016     <a href="http://www.fangraphs.com/statss.aspx?playerid=785">Todd Frazier</a>   CHW   666 0.096096096
## 8    2016 <a href="http://www.fangraphs.com/statss.aspx?playerid=791">Brandon Phillips</a>   CIN   584 0.030821917
## 9    2016   <a href="http://www.fangraphs.com/statss.aspx?playerid=1101">Ichiro Suzuki</a>   MIA   365  0.08219178
## 10   2016   <a href="http://www.fangraphs.com/statss.aspx?playerid=1159">Andrew Romine</a>   DET   194 0.067010309
## # ... with 352 more rows, and 12 more variables: `K%` <chr>, `BB/K` <chr>, AVG <chr>, OBP <chr>, SLG <chr>, OPS <chr>,
## #   ISO <chr>, BABIP <chr>, wRC <chr>, wRAA <chr>, wOBA <chr>, `wRC+` <chr>