In September 2017, I found myself working on a project that required odds data for football. At the time I didn’t know about resources such as Football-Data or the odds-api, so I decided to build a scraper to collect data directly from the bookmakers. However, most of them used JavaScript to display their odds, so I couldn’t collect the data with R and rvest alone. In this article, I’ll demonstrate how PhantomJS can be used with R to scrape JS-rendered content from the web.
Requirements
In addition to R’s base packages, I’ll need the following for this example:
-
rvest – This package for R is a wrapper for xml2 and httr packages. It allows you to download and extract data from HTML and XML.
-
PhantomJS – A headless browser that can be used to access webpages and extract information from them, among other things.
-
Selector Gadget – This one is optional, but it makes life easier because it isn’t necessary to go through the source code to find the XPath and/or CSS selectors for extracting the information you need from an HTML document.
Collecting Data
For this example I’ll stick to the original intent of the project and collect odds from an online bookmaker. Paddy Power (no affiliation) uses JavaScript and also doesn’t have an API to my knowledge, so I’ll collect odds from their website.
Without PhantomJS
To collect odds data with rvest, we need to:
-
Read the HTML file from a URL.
-
Extract the HTML elements containing the odds. (The `` elements that contain the data I am interested in have the attribute
class="avb-item"
.) -
Extract the contents of the elements.
-
Clean the data if necessary.
elements that contain the data I am interested in have the attribute class="avb-item"
.)
- Extract the contents of the elements.
- Clean the data if necessary.
withoutJS <- xml2::read_html(“https://www.paddypower.com/football?tab=today”) %>%
- rvest::html_nodes(“.avb-item”) %>%
- rvest::html_text()
withoutJS character(0)
We ended up with an empty character vector because the HTML document contains only JS snippets in the source code instead of the content we see in the browser. That means the CSS selector we can see in the browser does not exist in the document R is trying to extract data from.
With PhantomJS
The first step is to save the following script in a separate file:
1 |
|
The purpose of this script is to retrieve the HTML file from the specified URL and store it into a local HTML file, so that R can read contents from that file instead of reading the contents directly from the URL.
We can then use the system()
function to invoke the script from R and repeat the steps outlined in the attempt without PhantomJS to extract the data:
1 |
|
Note for Windows users: The invocation of the script has the same format as the command for Linux, but you may have to add .exe to phantomjs for it to work.
The content is currently a mess and the variable is one large chunk of text, i.e. a character vector of length 1. If I remove the white space and split it into individual elements, here’s what I get:
1 |
|
There’s still some useless data mixed in, and there is no guarantee that I can turn this vector into a data frame to store and analyze the data. In this particular case, team names can have more than two words, so I should extract information about matches and odds separately:
1 |
|
Now I can further transform the data before storing and analyzing it, e.g. convert fractional odds into decimal, split the Match column into “Home Team” and “Away Team” columns, and so on.
Although the script I used still works today, I should note that the development of PhantomJS is currently suspended and its git repository is archived. However, there are many ways to collect data from a JavaScript-rendered website, so you can also try using rvest and V8 from R or Python and Selenium to collect the data you need. Good luck!
Related