How to count selections in XPath and why?

by scrapecrow Apr 19, 2023

Counting selection can be a very powerful feature in XPath when parsing web scraped HTML pages.
Selection count can be used to navigate complex trees where parsing by attributes or values is not possible.

For this count() function can be used. See these interactive examples:

<!-- select only paragraphs related to product data but not metadata --> <article> <h2>Data</h2> <!-- this section contains product information --> <p>price: 199</p> <p>year: 2023</p> <h2>Data</h2> <!-- this section contains meta information --> <p>reviews: good</p> <p>updated: yesterday</p> </article>

The count() function is a great way to navigate bare HTML trees that otherwise might appear unparsable.

Related Articles

How to Parse XML

In this article, we'll explain about XML parsing. We'll start by defining XML files, their format and how to navigate them for data extraction.

PYTHON
CSS-SELECTORS
XPATH
DATA-PARSING
How to Parse XML

Ultimate XPath Cheatsheet for HTML Parsing in Web Scraping

Ultimate companion for HTML parsing using XPath selectors. This cheatsheet contains all syntax explanations with interactive examples.

XPATH
DATA-PARSING
Ultimate XPath Cheatsheet for HTML Parsing in Web Scraping

Web Scraping With Ruby

Introduction to web scraping with Ruby. How to handle http connections, parse html files for data, best practices, tips and an example project.

RUBY
HTTP
DATA-PARSING
CSS-SELECTORS
XPATH
INTRO
Web Scraping With Ruby

Parsing HTML with Xpath

Introduction to xpath in the context of web-scraping. How to extract data from HTML documents using xpath, best practices and available tools.

DATA-PARSING
PARSEL
XPATH
PYTHON
Parsing HTML with Xpath

Web Scraping With PHP 101

Introduction to web scraping with PHP. How to handle http connections, parse html files for data, best practices, tips and an example project.

PHP
HTTP
DATA-PARSING
XPATH
INTRO
Web Scraping With PHP 101

Web Scraping With Scrapy: The Complete Guide in 2025

Tutorial on web scraping with scrapy and Python through a real world example project. Best practices, extension highlights and common challenges.

PYTHON
SCRAPY
FRAMEWORK
XPATH
INTRO
Web Scraping With Scrapy: The Complete Guide in 2025