Are you looking to extract specific pieces of text from websites for data analysis, research, or other purposes? Web scraping using Python is a powerful technique that allows you to programmatically gather information from web pages. In this in-depth guide, we‘ll walk through how to get text from websites using the LXML library and XPath selectors.
What are LXML and XPath?
LXML is a fast and feature-rich Python library for parsing XML and HTML documents. It provides an intuitive way to navigate and extract data from web pages using XPath, a query language for selecting nodes in XML/HTML documents.
Some key benefits of using LXML for web scraping include:
- Very fast parsing of large documents
- Extensive support for XPath to precisely select desired elements
- Ability to handle messy or broken HTML
- Easy integration with other Python libraries like Requests
XPath is the real magic behind LXML‘s web scraping capabilities. It provides a way to specify which parts of an HTML document you want to extract. XPath expressions can be used to navigate the document tree, select nodes based on criteria, and extract text values, attributes, and more.
Step-by-Step Guide to Extracting Text with LXML
Now that you have a high-level understanding of LXML and XPath, let‘s dive into the step-by-step process of using them to get text from a webpage. We‘ll be using Python 3 in these examples.
Step 1: Install LXML
First, make sure you have LXML installed. You can install it using pip:
pip install lxml
Step 2: Make an HTTP request
Use the Requests library to fetch the webpage you want to scrape. For this example, we‘ll scrape https://proxyway.com/, a site with reviews of top proxy providers.
import requests
url = ‘https://proxyway.com/‘
page = requests.get(url)
Step 3: Parse the HTML
Next, parse the raw HTML from the page using LXML‘s HTML parser:
from lxml import html
tree = html.fromstring(page.content)
The tree object represents the parsed HTML as a tree structure we can now query with XPath.
Step 4: Select elements with XPath
Now the fun part – using XPath to select the pieces of text you want to extract. LXML supports a wide range of XPath expressions for precisely targeting elements.
Let‘s say we want to get the names of the top proxy providers reviewed on the site. Inspecting the HTML source, we can see the provider names are in <h3> tags inside <div class="item"> containers.
Here‘s an XPath expression to select those <h3> elements:
providers = tree.xpath(‘//div[@class="item"]/h3/text()‘)
Breaking this down:
//selects nodes anywhere in the documentdiv[@class="item"]selects<div>elements with a class of "item"/h3selects<h3>elements directly inside those divs/text()selects the text inside the<h3>tags
Step 5: Extract the text
The providers variable now contains a list of the extracted text strings. We can print them out to see the results:
for provider in providers:
print(provider)
This will output:
Bright Data IPRoyal Proxy-Seller SOAX Smartproxy ...
Congratulations, you‘ve just scraped your first piece of text from a webpage using LXML and XPath! The same basic process can be used to extract any other text elements from the page.
Advanced XPath Techniques
XPath provides many powerful ways to select elements. Here are a few more advanced techniques to add to your web scraping toolkit:
Selecting elements by class when there are multiple classes
Sometimes an element will have multiple classes, so a simple @class="classname" selector won‘t work. In this case, you can use the contains() function.
For example, to select <div> elements that have a class that contains "item":
items = tree.xpath(‘//div[contains(@class, "item")]‘)
Selecting sibling elements
XPath axes like following-sibling let you select elements based on their relative position in the document tree.
For instance, to select the <p> element following a <div> with id="description":
desc = tree.xpath(‘//div[@id="description"]/following-sibling::p/text()‘)
Handling errors with try/except
Web pages don‘t always have the structure we expect. It‘s a good practice to use try/except blocks to handle cases where an element doesn‘t exist or doesn‘t have the attributes we‘re looking for.
try:
desc = tree.xpath(‘//div[@id="description"]/p/text()‘)[0]
except IndexError:
desc = ‘No description found.‘
Using Proxies with LXML
When scraping websites, it‘s important to be respectful and avoid overloading servers with requests. Using proxies lets you distribute your requests from different IP addresses.
Here are some of the top proxy providers that work well with LXML and Python:
- Bright Data – largest proxy network with over 72M+ IPs
- IPRoyal – residential, datacenter, and mobile proxies with flexible plans
- Proxy-Seller – affordable residential and mobile proxy packages
- SOAX – reliable residential proxies with user:pass and IP auth
- Smartproxy – residential proxies optimized for scraping with high success rates
- Proxy-Cheap – affordable residential proxies with worldwide locations
- HydraProxy – fast dedicated and residential proxies with API access
To make requests through a proxy with Python, set the proxies parameter when calling requests.get():
proxies = {‘http‘: ‘http://user:[email protected]:8888‘,
‘https‘: ‘http://user:[email protected]:8888‘}
page = requests.get(url, proxies=proxies)
Using a pool of proxies and rotating them between requests will help keep your scraping undetected and efficient.
Conclusion
Extracting text from websites using LXML and XPath is a powerful skill for any web scraper‘s toolkit. With the techniques covered in this guide, you‘ll be able to precisely select and extract any pieces of data you need from web pages.
Remember to always be respectful when scraping websites and use proxies to avoid over-burdening servers. The proxy providers mentioned above are great options to keep your scraping fast and reliable.
Now go forth and put your new LXML and XPath skills to use! With a little practice and experimentation, you‘ll be a pro at parsing websites and extracting valuable data in no time.

