Response

统一的 HTTP 响应对象,内置选择器解析能力。

属性

属性类型说明
urlstr最终响应 URL(重定向后)
status_codeintHTTP 状态码
okbool状态码是否在 2xx 范围
headersdict[str, Any]响应头
cookiesdict[str, str]本次响应的 cookies
client_cookiesdict[str, str]会话级 cookies
contentbytes响应体原始字节
textstr响应体文本
json_datadict | list | NoneJSON 数据(解析失败为 None)
request_dataRequestModel原始请求信息
domainstr域名(含协议),如 https://example.com
hoststr主机名,如 example.com

选择器方法

css

def css(query: str) -> SelectorList

执行 CSS 选择器查询。

resp.css("title::text").get()          # 第一个匹配
resp.css("li::text").getall()          # 所有匹配
resp.css("a::attr(href)").get()        # 属性值
resp.css("div.none::text").get("默认")  # 默认值

xpath

def xpath(query: str) -> SelectorList

执行 XPath 查询。

resp.xpath("//title/text()").get()
resp.xpath("//a/@href").getall()
resp.xpath("//div[@class='item']/text()").get()

re

def re(regex: str, replace_entities: bool = True) -> list[str]

正则匹配,返回所有匹配组。

resp.re(r"价格: (\d+)元")  # => ["99", "199"]

re_first

def re_first(regex: str, default=None, replace_entities: bool = True) -> str | None

正则匹配,返回第一个结果。

resp.re_first(r"价格: (\d+)元")               # => "99"
resp.re_first(r"不存在 (\d+)", default="N/A")  # => "N/A"

jmespath

def jmespath(expression: str) -> Any

对 JSON 响应执行 JMESPath 查询。json_data 为 None 时返回 None。

resp.jmespath("data[0].name")              # 嵌套取值
resp.jmespath("data[*].name")              # 通配符
resp.jmespath("data[?age > `18`].name")    # 条件过滤
resp.jmespath("data[*].[name, age]")       # 多字段

to_url

def to_url(urls: list[str] | str) -> list[str]

将相对路径转为绝对路径。已是 http 开头的会被过滤。

resp.to_url("/page")                       # => ["https://example.com/page"]
resp.to_url(["/a", "https://other.com"])   # => ["https://example.com/a"]

SelectorList 方法

css()xpath() 返回的 SelectorList 对象:

方法说明
.get(default=None)获取第一个结果
.getall()获取所有结果
.re(regex)正则匹配
.re_first(regex)正则匹配第一个
.css(query)链式 CSS 选择
.xpath(query)链式 XPath 选择