Fundamentals 6 min read

Master XPath: Essential Node Selection, Predicates, and Functions for Web Scraping

This guide explains the core XPath syntax—including node selectors, predicates, wildcards, multiple paths, axes, and built‑in functions—providing clear examples so you can efficiently locate and filter XML/HTML elements when scraping web pages.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master XPath: Essential Node Selection, Predicates, and Functions for Web Scraping

Node Selection

XPath provides several shortcuts for selecting nodes: nodename – selects all child nodes of the given name, e.g.,

xpath('//div')
/

– selects from the document root, e.g.,

xpath('/div')
//

– selects all matching nodes regardless of depth, e.g.,

xpath('//div')
.

– selects the current node, e.g.,

xpath('./div')
..

– selects the parent of the current node, e.g.,

xpath('..')
@

– selects an attribute, e.g.,

xpath('//@class')

Predicates

Predicates, placed inside square brackets, filter nodes based on position, attribute values, or other conditions. xpath('/body/div[1]') – first div under

body
xpath('/body/div[last()]')

– last div under

body
xpath('/body/div[last()-1]')

– second‑last

div
xpath('/body/div[position()<3]')

– first two div elements xpath('/body/div[@class]')div elements that have a class attribute xpath('/body/div[@class="main"]')div with

class="main"
xpath('/body/div[price>35.00]')

div whose price child is greater than 35

Wildcards

Wildcards let you match unknown elements: xpath('/div/*') – all child nodes of a

div
xpath('/div[@*]')

– all div elements that have any attribute

Multiple Paths

Use the pipe operator | to combine paths: xpath('//div|//table') – selects every div and table node

XPath Axes

Axes define node sets relative to the current node: ancestorxpath('./ancestor::*') selects all ancestors (parents, grandparents) ancestor-or-selfxpath('./ancestor-or-self::*') selects ancestors and the node itself attributexpath('./attribute::*') selects all attributes of the node childxpath('./child::*') selects all child nodes descendantxpath('./descendant::*') selects all descendants (children, grandchildren, …) followingxpath('./following::*') selects nodes after the current node’s end tag following-siblingxpath('./following-sibling::*') selects following sibling nodes parentxpath('./parent::*') selects the parent node precedingxpath('./preceding::*') selects nodes before the current node’s start tag preceding-siblingxpath('./preceding-sibling::*') selects preceding sibling nodes selfxpath('./self::*') selects the current node itself

Functions

Built‑in functions enable more flexible queries: starts-withxpath('//div[starts-with(@id,"ma")]') selects div elements whose id starts with “ma” containsxpath('//div[contains(@id,"ma")]') selects div elements whose id contains “ma” andxpath('//div[contains(@id,"ma") and contains(@id,"in")]') selects div elements whose id contains both “ma” and “in” text()xpath('//div[contains(text(),"ma")]') selects div elements whose text contains “ma”

For more details, see the Scrapy XPath documentation: http://doc.scrapy.org/en/0.14/topics/selectors.html

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

XMLData ExtractionWeb ScrapingXPathSelectors
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.