You are currently reading XPath for Web Scraping: A Practical, Hands-On Guide to Web Scraping with XPath — a free, online book that is designed to get developers up-and-running with XPath as quickly and as easily as possible.
The magic of XPath is best experienced, rather than explained, which is why the bulk of this book is based around practical examples and lessons. If you’re not sure if XPath is worth your time though, I’m happy to put on my salesman hat and convince you that, yes, if you’re serious about web scraping, you need to know how to write XPath expressions.
To illustrate this flexibility, consider the following scenarios:
hrefvalue of every link on a page that has the words “Buy Now” in the anchor text.
srcattributes of the first
imgelement that appears inside a
divelement with a class of “content”
In theory, these sound like relatively complicated things to achieve, but XPath can handle all of these situations without breaking a sweat. In fact, these are some of the easiest things we can do with XPath. Once we dive into the more advanced details, what we can achieve is absolutely mind-boggling.
XPath expressions don’t simply make it easier to scrape the web. They allow us to avoid writing a ton of repetitive code. When you consider how volatile web scraping can be – the web is constantly changing, after all – this ability to write less code becomes an incredible advantage.
For example, think about how much time it’d take to handle the scenarios we just discussed using the programming language of your choice, or a visual scraping tool (like import.io). Then know that we can achieve all of that same functionality with three, relatively short XPath expressions:
href attribute of “Buy Now” links:
src of first image inside
Extract all rows of a table, except for the first and last rows:
//table//tr[position()>1 and position()<last()]
You don’t have to understand what these expressions are actually doing – we’ll uncover their mysteries soon enough – but at least appreciate how much functionality we’re able to achieve in less than fifty characters.
Let me know if this sounds familiar:
Even if you’re new to the world of web scraping, it’s not hard to imagine battling this scenario on a fairly regular basis. The web is a constantly shifting beast, and it’s this lack of stability that ensures web scraping remains an inherently fragile process.
The bad news is, we can’t avoid this fragility altogether (unless we only scrape websites that literally never change), but there is some good news:
XPath has a number of features that allow us to scrape content in a far more reliable manner, so even if a website does change, there is less of a risk that the our scrapers will completely fall apart. This does require a bit of luck (or psychic powers), but since these features are so easy to implement, there’s plenty of upside for only a minimal investment of time.
Okay, this might not look readable:
In fact, it probably looks downright cryptic. But once you learn the basic rules of how XPath expressions are structured – and don’t worry, there aren’t many rules to learn – you’ll see XPath for what it truly is: a wonderfully expressive syntax that can be as easy to read as your native tongue.
If you’re skeptical of this point, that’s fair enough. When I first came across XPath, it seemed like impenetrable gibberish. If you stick with it until the end of this book though, you’ll have a strong grasp of any XPath expression that you come across.
Some web scraping tools, like Python’s BeautifulSoup library, have their own syntax for extracting content from the web, but throughout the web scraping world, you’ll find that XPath is the standard. It’s supported by a ton of scraping libraries, tools, and applications, including:
The benefit of this ubiquity is that, once you learn XPath, your knowledge of the syntax will carry over between countless platforms. You want be locked into any specific, proprietary way of working, so if you ever want to move to a different set of tools, you’ll be able to simply copy and paste your XPath expressions and they should work exactly the same.
Many web scraping tools that support XPath expressions also allow users to extract content from the web using CSS selectors, and there’s a couple of significant reasons you might prefer to use CSS instead of XPath:
To illustrate the second point, imagine that we want to grab all of the elements from a web page that have a class of “button” by using the CSS features of the Nokogiri library. Here’s how we’d write that code:
Pretty slick, right?
In contrast, this is how we’d achieve the same effect with XPath:
The syntax, clearly, isn’t as seamless.
The problem with CSS is that you’re limited with the amount of logic you can write into your expressions. You can’t, for instance, use CSS to grab all of the
href attributes from all of the links on a page, and you couldn’t extract the second-last row of data from the first table on a page. You could select all of the links on a page, and select that first table, but you’d have to write (and maintain) additional code for the rest of the logic.
It’s for precisely this reason that you need to learn XPath if you’re serious about web scraping. If you only have knowledge of using CSS selectors, you’ll quickly find yourself limited (and frustrated) by what you can achieve.
That’s not to say CSS doesn’t have its place. I still use CSS in my web scrapers on a regular basis. It’s quick, easy, and aesthetically pleasing, so it’s a handy tool to have in your toolkit. The moment I need to do anything remotely sophisticated though, I reach for XPath.