Jordan Does Advanced CSS Selectors
Tags: webscraping, cssselectors
Published: 2020-07-21 11:45:59

---
title: Jordan Does Advanced CSS Selectors
published: true
date: 2020-06-01 13:05:00 UTC
tags: WebScraping,cssselectors,Webscraping
canonical_url: https://javascriptwebscrapingguy.com/jordan-does-advanced-css-selectors/?utm_source=rss&utm_medium=rss&utm_campaign=jordan-does-advanced-css-selectors
---

[Demo code here](https://github.com/aarmora/jordan-scrapes-rcdb)

Today I’m going back to the [Roller Coaster Database](https://rcdb.com/). I had a doctoral student reach out asking for some help with scraping the rcdb and he was using my code from my [original post](https://javascriptwebscrapingguy.com/jordan-scrapes-the-roller-coaster-database/) about it. It kind of led to me having to use some advanced techniques for CSS selecting while web scraping and that’s what I’d like to highlight today.

## Fragility of web scraping

![fragile gif](https://media.giphy.com/media/7l8T0LWhXSYhi/giphy.gif)

Web scraping code is extremely fragile. This post and story showcases that very well. This doctoral student reached out, saying that he couldn’t get the code to work. As I looked at his error, I could see that something had changed on the RCDB side.

This is the danger and pain of web scraping. We don’t control the source code. Any time the source html structure is changed, it has a high chance to break the CSS selectors we were previously using.

Web scraping code is going to require regular maintenance. In light of that, it’s best to build it in a way that expects that so this maintenance can be done quickly and with little pain.

## “Smart” CSS Selectors

![smort fun gif](https://media.giphy.com/media/hFROvOhBPQVRm/giphy.gif)

This doctoral student also asked if it was possible to grab a few additional fields. These fields, to be specific:

![rcdb fields example](https://javascriptwebscrapingguy.com/wp-content/uploads/2020/05/image-5.png)

![rcdb fields example](https://javascriptwebscrapingguy.com/wp-content/uploads/2020/05/image-6.png)

![rcdb fields example](https://javascriptwebscrapingguy.com/wp-content/uploads/2020/05/image-7.png)

See a problem yet? A common technique when using CSS selectors while web scraping is to just find the nth element. So I’m making the assumption that that field (inversions, for example) will always be at the same location. In the above examples, inversions is the 1st, 5th, and 2nd row, making my assumption incorrect.

What’s the solution? Well, in this case, and often this will work, is to try and make the code smart. Fields of data almost always have labels and when it’s in a table format like this it normally follows the same format. Here’s what the code looks like:

```

	$('section:nth-of-type(2) .stat-tbl tr').toArray().map(element => {
		let header = $(element).find('th').text();
		header = camelize(header);
		if (header === 'inversions' || header == 'duration') {
			const span = $(element).find('td').text();
			if (!undesirableStats.includes(header)) {
				rollerCoaster[header] = span;
			}
		}
		else {
			const span = $(element).find('span').text();
			if (!undesirableStats.includes(header)) {
				rollerCoaster[header] = span;
			}
		}
	});
```

I go ahead and grab all the rows in this stats table (found by using the CSS selector `section:nth-of-type(2) .stat-tbl tr`) and then loop through each. I take the header value, use a [neat function](https://stackoverflow.com/a/2970667/2287595) to turn it into camel case, and then I just use that as the key in my object.

The HTML I’m pulling from looks like this:

![html structure](https://javascriptwebscrapingguy.com/wp-content/uploads/2020/05/image-8.png)

## Still Fragile!

![fragile skeleton gif](https://media.giphy.com/media/26gR2hktpYoxD0Q7K/giphy.gif)

You can see in the code above that I have a conditional in there. Originally I did not. This point reemphasizes how fragile and kind of crummy web scraping code can be. I’m hard coding fields and changing how my CSS selectors work based on the header values.

All of this because I am at the mercy of the source code. For the fields of ‘Inversions’ and ‘Duration’ the HTML structure is different.

![css selectors are different for inversions and duration](https://javascriptwebscrapingguy.com/wp-content/uploads/2020/05/image-9.png)

If you look at speed, the value I want is within a `span`. For inversions, there is no `span`. So my original code broke and I had to set up a condition where it uses different CSS selectors based on the different field. Kind of a bummer.

And…that’s it. It’s a neat way to try and be smart when the values you’re selecting aren’t always in the same spot.

[Demo code here](https://github.com/aarmora/jordan-scrapes-rcdb)

## Looking for business leads?

Using the techniques talked about here at [javascriptwebscrapingguy.com](https://javascriptwebscrapingguy.com), we’ve been able to launch a way to access awesome business leads. Learn more at [Cobalt Intelligence](https://cobaltintelligence.com)!

The post [Jordan Does Advanced CSS Selectors](https://javascriptwebscrapingguy.com/jordan-does-advanced-css-selectors/) appeared first on [JavaScript Web Scraping Guy](https://javascriptwebscrapingguy.com).