r - 使用 R 和 rvest 抓取 Web 活动图表;overbuff.com
问题描述
该网站overbuff.com
允许跟踪您在《守望先锋》中的游戏历史,并提供活动图表。这个图表是基于一个复杂的html
,这阻碍了我收集我的数据的尝试R
。不幸的是,我在 html、CSS 和网页设计方面的技能仅处于初学者水平。
我给出了低于最低限度的reprex,以overbuff.com上当前排名第一的玩家为例。
网站:https://www.overbuff.com/players/pc/FunnyAstro-2570/activity
日期:**2016-06-30**
当您将鼠标悬停在绿色圆圈上时,class="day-popper"会显示当天的信息(输赢、字符)。但是,只有在您这样做之后,该信息才能在站点的源代码中获得。 插图:悬停
使用 Google Chrome,我提取了 XPath、CSS 选择器并尝试了 SelectorGadget。小工具只选择了没有信息的绿色圆圈。
这是获取日期的代码(2016-06-30)
# Example with the top-ranked player at the moment
library(rvest)
url <- "https://www.overbuff.com/players/pc/FunnyAstro-2570/activity"
# Test: Website is correctly received
mydata <- read_html(url) %>%
html_nodes("h1") %>%
html_text()
print(mydata)
# Xpath
mydata <- read_html(url) %>%
html_nodes(xpath="/html/body/div[1]/div[3]/div/div[2]/div/div/div/section[1]/article/div/div[1]/div[2]/div/div[27]/div[6]/div[2]/div/div/div[1]/div[1]") %>%
html_text()
print(mydata)
# Null
# CSS Selector
mydata <- read_html(url) %>%
html_nodes(xpath="body > div.container.seemsgood > div.row.layout-content > div > div:nth-child(2) > div > div > div > section.activity-date-section > article > div > div:nth-child(1) > div.chart-wrapper > div > div:nth-child(27) > div:nth-child(6) > div.day-popper > div > div > div.sessions-header > div.sessions-date") %>%
html_text()
print(mydata)
# Error
如你所见,我没有成功。为了说明网站的结构,您可以在此处找到这一天的 html 片段。
<div class="day-popper" data-placement="bottom" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(-91px, 105px, 0px);">
<div class="ActivitySessionsView">
<div class="sessions-container">
<div class="sessions-header">
<div class="sessions-date">2016-06-30</div>
<div class="sessions-record"><span class="color-stat-win">17</span><span class="sep">-</span><span class="color-stat-loss">4</span></div>
</div>
<div class="session-detail mode-competitive">
<div class="session-header">
<div class="session-stat session-mode">
<div class="value">Competitive<span class="small"> S1</span><i class="session-mode-icon fa fa-trophy"></i></div>
<div class="label">updated at 7:21 AM</div>
</div>
<div class="session-stat session-record">
<div class="value"><span class="color-stat-win">17</span><span class="sep">-</span><span class="color-stat-loss">4</span></div>
<div class="label">Record</div>
</div>
</div>
<div class="session-heroes">
<div class="session-hero"><img class="image-hero image-hero-lucio image-small" src="/assets/images/heroes/lucio.png?v=8f65c97" alt="Lúcio"><small><span class="color-stat-win">12</span><span class="sep">-</span><span class="color-stat-loss">0</span></small></div>
<div class="session-hero"><img class="image-hero image-hero-mercy image-small" src="/assets/images/heroes/mercy.png?v=8f65c97" alt="Mercy"><small><span class="color-stat-win">2</span><span class="sep">-</span><span class="color-stat-loss">0</span></small></div>
<div class="session-hero"><img class="image-hero image-hero-pharah image-small" src="/assets/images/heroes/pharah.png?v=8f65c97" alt="Pharah"><small><span class="color-stat-win">1</span><span class="sep">-</span><span class="color-stat-loss">0</span></small></div>
<div class="session-hero"><img class="image-hero image-hero-torbjorn image-small" src="/assets/images/heroes/torbjorn.png?v=8f65c97" alt="Torbjörn"><small><span class="color-stat-win">1</span><span class="sep">-</span><span class="color-stat-loss">0</span></small></div>
</div>
</div>
</div>
</div>
</div>
我想在所有可用的日子里提取这些信息并伪造成以下格式。
library(tibble)
tibble(time=rep("2016-06-30",4),
hero=c("Lucio", "Mercy","Phara","Torbjörn"),
win=c(12,2,1,1),
loss=c(0,0,0,0)
)
输出
# A tibble: 4 x 4
time hero win loss
<chr> <chr> <dbl> <dbl>
1 2016-06-30 Lucio 12. 0.
2 2016-06-30 Mercy 2. 0.
3 2016-06-30 Phara 1. 0.
4 2016-06-30 Torbjörn 1. 0.
解决方案
使用 CSS 选择器时请注意属性,因此对于类,您使用点“。” 作为前缀,例如
mydata <- read_html(url) %>%
html_nodes(".sessions-date") %>%
html_text()
print(mydata)
肯定会为您提供日期,您也可以使用“#”作为“id”。
如果选择器的类有多个项目,您将获得一个向量。
我希望这有帮助。