Introduction
In this post, we will attempt to collect some data about the mobile game Fire Emblem Heroes by Intelligent Systems and Nintendo. To do this, we’ll use the rvest package to scrape data from the Fire Emblem Heroes Gamepedia page
Loading packages
library("rvest")
Scraping data
Fire Emblem Heroes is a tactical RPG game in which players compete against the AI by trying to defeat their teams, composed of up to four characters (heroes). We’ll start then by collecting information on each available character, from the page assigned to the url variable:
url <- "https://feheroes.gamepedia.com/Stats_Table"
webpage <- read_html(url)
We can use the html_table function to extract the table, so let’s start there:
max_stats <- webpage %>%
html_table()
max_stats <- as.data.frame(max_stats)
head(max_stats)
## X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
## 1 NA Abel: The Panther NA NA 39 33 32 25 25 154
## 2 NA Alfonse: Prince of Askr NA NA 43 35 25 32 22 157
## 3 NA Alfonse: Spring Prince NA NA 41 35 33 30 18 157
## 4 NA Alm: Hero of Prophecy NA NA 45 33 30 28 22 158
## 5 NA Amelia: Rose of the War NA NA 47 34 34 35 23 173
## 6 NA Anna: Commander NA NA 41 29 38 22 28 158
As we can see, the function worked fine, except some of the things we want, namely the hero’s weapon and movement types are given as images, meaning that we’ll have to do just a bit more work to get that info.
Let’s get the table information again, but this time, we’ll do it in a bit more roundabout fashion. The information we want, that is, a hero’s weapon and movement types, as well as their stats, are contained in the <tr> elements of the html. Therefore, we can use rvest’s html_nodes function to extract the relevant nodes. This can then be passed to the html_attr function to extract the “data-weapon-type” and “data-move-type” attributes of the <tr> elements and include them back into our data.frame.
max_stats$WeaponType <- read_html(url) %>%
html_nodes("tr") %>%
html_attr("data-weapon-type")
max_stats$MoveType <- read_html(url) %>%
html_nodes("tr") %>%
html_attr("data-move-type")
Let’s just tidy up our max_stats data.frame. Note that I’ve changed the original “Total” column’s name to “BST”, for “base stat total”.
max_stats <- Filter(function(x)!all(is.na(x)), max_stats)
names(max_stats) <- c("Name", "HP", "ATK", "SPD", "DEF", "RES",
"BST", "WeaponType", "MoveType")
head(max_stats)
## Name HP ATK SPD DEF RES BST WeaponType MoveType
## 1 Abel: The Panther 39 33 32 25 25 154 Blue Lance Cavalry
## 2 Alfonse: Prince of Askr 43 35 25 32 22 157 Red Sword Infantry
## 3 Alfonse: Spring Prince 41 35 33 30 18 157 Green Axe Cavalry
## 4 Alm: Hero of Prophecy 45 33 30 28 22 158 Red Sword Infantry
## 5 Amelia: Rose of the War 47 34 34 35 23 173 Green Axe Armored
## 6 Anna: Commander 41 29 38 22 28 158 Green Axe Infantry
There are a few other tables with information about the game’s characters on the site, but for now this information is enough to get started. In my next post, I’ll start doing some exploratory analyses and see what kind of data we got.