🚀 We are hiring! See open positions

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:

python
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:

python
from lxml import html

tree = html.fromstring('<div class="price">22.85</div>')
print(tree.xpath("//div[@class='price']/text()"))
"22.85"
Scale Your Web Scraping
Anti-bot bypass, browser rendering, and rotating proxies — all in one API. Start with 1,000 free credits.
No credit card required 1,000 free API credits Anti-bot bypass included
Not ready? Get our newsletter instead.