Introduction to the Scrapy Selector in Python 2026
The Scrapy Selector is one of the most powerful and flexible tools for web scraping in Python. Built on top of parsel, it combines the best of CSS selectors and XPath, making it extremely efficient for extracting structured data from HTML and XML documents.
In 2026, the Scrapy Selector remains a core component of the Scrapy framework and is widely used even in standalone scripts due to its speed, readability, and advanced features. This March 24, 2026 guide introduces the Scrapy Selector with modern best practices.
TL;DR — Key Takeaways 2026
Selectorfrom Scrapy (based on parsel) supports both CSS and XPath- Use
.css()for simple and readable selections - Use
.xpath()for complex queries and attribute-heavy tasks - Extract text with
::textand attributes with::attr(name) - Works great both inside Scrapy spiders and in standalone scripts
- Very fast and memory-efficient compared to BeautifulSoup
1. Basic Setup and Usage
from scrapy.selector import Selector
import httpx
async def fetch_and_select(url: str):
async with httpx.AsyncClient() as client:
response = await client.get(url)
# Create Scrapy Selector
sel = Selector(text=response.text)
return sel
# Example usage
# sel = await fetch_and_select("https://example.com")
2. Extracting Data with CSS and XPath
# CSS Selectors
titles = sel.css("h1::text").getall()
prices = sel.css("span.price::text").getall()
links = sel.css("a.product-link::attr(href)").getall()
# XPath Selectors
first_title = sel.xpath("//h1/text()").get()
product_ids = sel.xpath("//div[@data-product-id]/@data-product-id").getall()
# Combined approach (recommended)
products = []
for item in sel.css("div.product-card"):
product = {
"title": item.css("h2.title::text").get().strip(),
"price": item.css("span.price::text").get().strip(),
"link": item.css("a::attr(href)").get(),
"rating": item.css("div.rating::text").get()
}
products.append(product)
3. Real-World Example – Product Listing Scraper
async def scrape_products(url: str):
sel = await fetch_and_select(url)
return [
{
"title": item.css("h2::text").get().strip(),
"price": item.css(".price::text").get().strip(),
"link": item.css("a::attr(href)").get(),
"image": item.css("img::attr(src)").get()
}
for item in sel.css("div.product")
]
4. Best Practices for Scrapy Selector in 2026
- Prefer CSS for most cases — cleaner and more readable
- Use XPath when you need complex conditions or parent/child navigation
- Chain selectors instead of writing long single expressions
- Use
.get()and.getall()— never assume elements exist - Combine with httpx + asyncio for standalone high-performance scrapers
- Always handle None values gracefully
Conclusion — Scrapy Selector in 2026
The Scrapy Selector remains one of the most elegant and powerful tools for web scraping in Python. Whether you are building a full Scrapy spider or a lightweight standalone scraper, mastering the Selector gives you speed, flexibility, and clean code. In 2026, using it with modern async HTTP clients makes it even more effective.
Next steps:
- Start using Scrapy Selector in your next scraping project
- Related articles: Setting up a Selector in Python 2026 • Extracting Data from a SelectorList 2026