Categories
Python Answers

How to use XPath with BeautifulSoup and Python?

Spread the love

To use XPath with BeautifulSoup and Python, we can replace BeautifulSoup with lxml.

For instance, we write

from lxml import html
import requests

page = requests.get('http://example.com')
tree = html.fromstring(page.content)
names = tree.xpath('//div[@title="name"]/text()')
prices = tree.xpath('//span[@class="price"]/text()')

print('Names: ', names)
print('Prices: ', prices)

to call html.fromstring to parse the HTML string into an object.

Then we call xpath with the XPath to get the items by XPath.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *