Selector的用法

Selector的用法

Selector是基于lxml构建的支持XPath选择器、CSS选择器,以及正则表达式,功能全面,解析速度和准确度非常高

python 复制代码
from scrapy import Selector

body = '<html><head><title>HelloWorld</title></head><body></body></html>'
selector = Selector(text=body)
title = selector.xpath('//title/text()').extract_first()
print(title)

单独将Selector拿出来使用 运行结果如下:

由于Selector主要是与Scrapy结合使用

如Scrapy的回调函数中的参数response直接调用xpath()或者css()方法来提取数据

这里用官方文档中的一列做演示

Selectors --- Scrapy 2.13.3 documentationhttps://doc.scrapy.org/en/latest/topics/selectors.html

在命令行使用这一行代码进入scrapy shell模式

scrapy shell https://doc.scrapy.org/en/latest/topics/selectors.html![](https://i-blog.csdnimg.cn/direct/186b36aab22841b5a6a80ca150358a60.png)

相关推荐
真智AI2 分钟前
用 LLM 辅助生成可跑的 Python 单元测试:pytest + coverage 覆盖率报告(含运行指令与排坑)
python·单元测试·pytest
zy_destiny11 分钟前
【工业场景】用YOLOv26实现桥梁检测
人工智能·深度学习·yolo·机器学习·计算机视觉·目标跟踪
2501_9418372614 分钟前
蘑菇可食用性分类识别_YOLO11分割模型实现与优化_1
人工智能·数据挖掘
2501_9418372614 分钟前
基于YOLO11-Aux改进的圣女果目标检测实现
人工智能·目标检测·计算机视觉
0思必得015 分钟前
[Web自动化] Selenium处理文件上传和下载
前端·爬虫·python·selenium·自动化·web自动化
莫有杯子的龙潭峡谷22 分钟前
在 Windows 系统上安装 OpenClaw
人工智能·node.js·安装教程·openclaw
Funny_AI_LAB24 分钟前
AI Agent最新重磅综述:迈向高效智能体,记忆、工具学习和规划综述
人工智能·学习·算法·语言模型·agi
zhangshuang-peta37 分钟前
超越Composio:ContextForge与Peta作为集成平台的替代方案
人工智能·ai agent·mcp·peta
Hui Baby38 分钟前
Java SPI 与 Spring SPI
java·python·spring
power 雀儿39 分钟前
Transformer输入嵌入与绝对位置编码
人工智能·深度学习·transformer