使用rvest从R中的crickbuzz中获取匹配分数

时间:2016-01-07 19:49:38

标签: r rvest

我正在抓取页面Crickbuzz scores以获取匹配详情。我正在使用选择器小工具来获取css标签。我到目前为止所做的事情是:

crickbuzz <- read_html(httr::GET("http://www.cricbuzz.com/cricket-match/live-scores"))
matches_dates <- crickbuzz %>%
html_nodes(".schedule-date:nth-child(1)") %>%
html_text()

我已经获得了比赛,分数和场地,但是在获取日期时遇到了困难。 我从上面的代码

得到以下结果
> matches_dates
     "   -     " "   -     " "   "       "   "       "   "       "   "   "  "      
    "   "       "   "       "   "       "   -     " "   -     " "   -     "

表示获得21个元素,这是正确的,因为当前有21个匹配,但没有获得文本。

然后我看到了html_nodes()中的内容 它给出了像:

{xml_nodeset (21)}
 1 <span class="schedule-date" timestamp="1452132000000" format="MMM dd'">    
   </span>
2 <span class="schedule-date" timestamp="1452132000000" format="MMM dd'">    
   </span>
3 <span class="schedule-date" timestamp="1452132000000" format="MMM dd'">    
   </span> and so on....

这意味着我没有从标签中获取文本。  怎么做?

1 个答案:

答案 0 :(得分:0)

您需要使用timestamp属性提取它:

library(rvest)
crickbuzz <- read_html(httr::GET("http://www.cricbuzz.com/cricket-match/live-scores"))
matches_dates <- crickbuzz %>%
    html_nodes(".schedule-date:nth-child(1)")%>%
   html_attr("timestamp")

matches_dates
 [1] "1452268800000" "1452132000000" "1452247200000" "1452242400000" "1452327000000" "1452290400000" "1452310200000" "1452310200000" "1452310200000"
[10] "1452310200000" "1452324600000" "1452324600000" "1452324600000" "1452324600000" "1452324600000" "1452150000000" "1452153600000" "1452153600000"

# this is the unix time and so if you need to convert to date-time format, follow the answer
 to this question: 
http://stackoverflow.com/questions/13456241/convert-unix-epoch-to-date-object-in-r