Can I used XPath selectors in BeautifulSoup?

No, Python's BeautifulSoup doesn't support XPath selectors despite supporting lxml backend which can perform XPath queries.

To use XPath selectors either lxml or parsel packages must be used.

parsel is a modern wrapper around lxml which makes xpath selections very easy:
from parsel import Selector

selector = Selector(text='<div class="price">22.85</div>')
print(selector.xpath("//div[@class='price']/text()").get())
"22.85"

Alternatively, lxml can be used directly:

from lxml import html

tree = html.fromstring('<div class="price">22.85</div>')
print(tree.xpath("//div[@class='price']/text()"))
"22.85"

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

How to Parse Web Data with Python and Beautifulsoup

Beautifulsoup is one the most popular libraries in web scraping. In this tutorial, we'll take a hand-on overview of how to use it, what is it good for and explore a real -life web scraping example.

BEAUTIFULSOUP
DATA-PARSING
PYTHON
How to Parse Web Data with Python and Beautifulsoup