Response parsing
Extract fields from an HTTP response in a spider.
Scrapy · Class
Selector wraps a response or markup string and lets you select document content with XPath or CSS. Results are extracted with get() or getall().
Selector wraps response content or supplied markup so XPath, CSS, and related extraction operations can be applied. When created from a response, Scrapy infers the selector type from that response; when created from text, it defaults to HTML unless a type is specified.
scrapy.Selector(**args: Any, **kwargs: Any)| Name | Type | Description |
|---|---|---|
response | TextResponse | None | A TextResponse or response-compatible object whose content is selected and extracted. |
text | str | None | Markup supplied directly when a response is unavailable. |
type | str | None | Parsing mode; supported values include html, xml, json, or None for inference. |
A Selector instance wrapping the supplied response or text content.
from scrapy import Selector
html = "<html><body><h1>Scrapy</h1></body></html>"
selector = Selector(text=html)
print(selector.xpath("//h1/text()").get())Creates a selector from markup and extracts the heading text with XPath.
Extract fields from an HTTP response in a spider.
Parse a saved HTML fragment or fixture without issuing a request.
Traverse nested matches using chained XPath or CSS selections.
Selector is Scrapy’s wrapper for selecting parts of response content. It can be constructed from a TextResponse or from markup passed through text.
Follow-up: How would you create one without a response? Pass the markup string using Selector(text=...).
Use xpath() or css() to locate content, then get() for one result or getall() for all matching results. XPath and CSS selections return selector objects that support further chaining.
Follow-up: What happens when no result is available from get()? It returns None, while getall() returns an empty list.
The selector type can be inferred from the response, or forced with type. Supported types include html, xml, and json, with HTML used as the default when text is supplied without an explicit type.
Follow-up: Why force type? To ensure the supplied content is parsed using the intended rules instead of automatic inference.
SelectorListResponse.selectorxpath()css()get()getall()parsel