Sometimes data comes in a nicely packaged format like a csv or a database table… and sometimes it doesn’t. So what do you do when you need to grab some data from a website, but there is no API available? Scrape It!
I’m an avid MMA fan and from time to time I want to check out some data on fighters. Sherdog.com provides data on fighters including:
- Win/Loss Record
- Specifics about each fight: opponent, result, referee, event, etc.
However Sherdog doesn’t have an API; this is where beautiful soup comes in. I ran a quick google search for Sherdog web scrapers and found one by Andrew Valish which was written in Node.js. I used this as a template and resource for the examples I provide below. NOTE: In case you are looking for a prebuilt solution using Python, there are a couple of Python based Sherdog scapers available on Github. (Please see References section for links)
Conor McGregor is fighting for the UFC Featherweight title at UFC 194 on December 12th against the reigning champ Jose Aldo. So let’s take a look at some of his stats. The first step is to import beautiful soup and then read the web page.
The initial contents of soup are shown below (using the soup.prettify() function)
Ok, so that doesn’t look very useful, but there are a variety of tools to help us sort out what pieces of data we want. The gif below shows how to view developer tools in Chrome. You can see as I move the cursor over the HTML elements they are highlighted in the page.
I’ll start with collecting the fighter name and nickname:
I reference the name and nickname using the css class in the html. I search soup for the fighter name via the find function using both the html element, span, and the css class name, fn. The section of HTML containing this text is shown below:
Similarly, I collect Conor’s birthday, weight, height, nationality, and association (team):
In this case I will identify the birth date by searching on the itemprop field birthDate. To isolate the fighter’s age I access the strong tag located in the item birthday class.
Sherdog also provides information about each of a fighter’s fights in a table. We can scrape this data as shown below:
The table is inside of a div of class module fight_history, but there is also another table which displays the amateur record of the fighter in a similar div. To identify the table with the professional record, I need to first identify the location of the text string Fight History which is the header for the table we want. Conversely the amateur record is labeled with the string Amateur Fights. Once the string is identified I locate the parent tag with a class of module fight_history and then search for a table element. To clarify, when using the find function you are searching down the html tree, but when using find_parent you are working your way up the tree. Below are snippets showing the similarity of the amateur and professional tables.
Next we want to go row by row and collect the information about each fight. The code below outlines how to do this. The rows of the table are identified by the css classes odd and even. For each row we isolate the desired data. For more information on iterating through tables check out this Stack Overflow post.
The first couple of results are shown below:
As I pointed out earlier, this was just an example to help highlight what can be done with beautiful soup. Check out the sources below for prebuilt solutions in both Python and Node.js (specifically for scraping Sherdog)