How to select elements by text in Xpath
In Python, you can use the lxml
library along with XPath to select elements by text content. Here's an example of how you can retrieve the title of a web page using lxml
and XPath with the website scrapingdog.com
as an example:
import requests
from lxml import html
# Send a GET request to the web page
response = requests.get('https://www.scrapingdog.com/')
# Parse the HTML content
page_content = html.fromstring(response.content)
# Extract the title using XPath
title = page_content.xpath('//title/text()')
# Print the title
print(title[0] if title else 'Title not found')
In the above example, we first send a GET request to the web page using the requests
library to retrieve the HTML content. Then, we parse the HTML content using html.fromstring()
from lxml
. Next, we use an XPath expression //title/text()
to select the text content of the <title>
element, which represents the title of the web page. Finally, we print the extracted title. Note that the [0]
indexing is used to access the first item in the list of selected elements, and the conditional expression title[0] if title else 'Title not found'
is used to handle cases where the title element is not found in the web page.