Slashes and Brackets in Web Scraping with Python 2026: XPath vs CSS Explained Introduction to the Scrapy Selector in Python 2026 Setting up a Selector in Python 2026: Best Practices for Web Scraping Selecting Selectors in Python 2026: Best Practices for Web Scraping Extracting Data from a SelectorList in Python 2026: Best Practices CSS Locators in Python 2026: Powerful Web Scraping Techniques Attributes in CSS Selectors for Web Scraping in Python 2026 Selectors with CSS in Python 2026: Modern Web Scraping Techniques Text Extraction in Python 2026: Modern Techniques & Best Practices Crawl in Python 2026: Building Modern Web Crawlers with Best Practices A Classy Spider in Python 2026: Building Web Crawlers with Elegance & Best Practices Nodriver Canvas Fingerprint Spoofing 2026 – Advanced Anti-Detection Techniques WebGL Fingerprint Spoofing Techniques 2026 – Advanced Python Web Scrapping Evasion AudioContext Fingerprint Spoofing Techniques 2026 – Advanced Python Web Scrapping Evasion WebGL + AudioContext Integration Spoofing Techniques 2026 – Advanced Python Web Scrapping Evasion Canvas + WebGL Integration Spoofing Techniques 2026 – Advanced Python Web Scrapping Evasion Web Scraping with Python 2026 Web Scraping with Python in 2026 – Complete Beginner to Advanced Guide Python Web Scraping Tutorial 2026 – Playwright, Scrapy & BeautifulSoup Guide Building Your First Scrapy Spider in 2026 – Modern Python Web Scrapping Guide View all in this category →

Introduction to the Scrapy Selector in Python 2026

Web Scraping Mar 24, 2026

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

  • Selector from 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 ::text and 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
# For this demo, build the Selector from sample HTML
# (in your project: sel = await fetch_and_select(url))
sel = Selector(text="""
<h1>Spring Sale</h1>
<div class="product-card" data-product-id="101">
  <h2 class="title"> Laptop Pro 14 </h2><span class="price"> $1,299 </span>
  <a class="product-link" href="/p/101">View</a><div class="rating">4.7</div>
</div>
""")

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)

print(titles, product_ids)
print(products)

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

Why Use Scrapy's Selector on Its Own

Scrapy's Selector class comes from the parsel library and can be used without running a full Scrapy project. You give it HTML text and then query it with CSS or XPath, which makes it a lightweight choice for parsing pages you already downloaded or saved.

Every query returns a SelectorList. Call .get() for the first result or None, and .getall() for a list of all results. Chaining is allowed, so you can select product cards first and then query inside each card, which keeps fields from different products from mixing.

The same Selector code later works unchanged inside a Scrapy spider, where response.css and response.xpath use the same API.

Worked Example: Parsing a Saved Page Into Records

We parse a small saved listing into a list of dictionaries, querying inside each card.

from scrapy.selector import Selector

saved = """
<article class="card"><h3>Laptop</h3><span class="price">999</span></article>
<article class="card"><h3>Mouse</h3></article>
"""

records = []
for card in Selector(text=saved).css("article.card"):
    records.append({
        "name": card.css("h3::text").get(),
        "price": card.css("span.price::text").get(default="n/a"),
    })
print(records)

The result has two records. The mouse has no price element, so get(default="n/a") returns n/a instead of None, and the loop over cards keeps each price next to the right name.

Common Mistakes

  • Calling .getall() on the whole page for each field and then zipping lists of different lengths.
  • Calling .strip() on .get() results that may be None.
  • Using the old .extract_first() style in new code instead of .get().
  • Downloading the same page many times while developing instead of saving it once.

Quick Summary

  • Selector parses HTML text with CSS or XPath.
  • .get() returns one value, .getall() a list.
  • Query inside each card to keep fields together.
  • The same API works in Scrapy spiders.

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 Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices