Scrapy Shell

Scrapy终端是一个交互终端,我们可以在未启动spider的情况下尝试及调试代码,也可以用来测试XPath或CSS表达式,查看他们的工作方式,方便我们爬取的网页中提取的数据。

如果安装了 IPython ,Scrapy终端将使用 IPython (替代标准Python终端)。 IPython 终端与其他相比更为强大,提供智能的自动补全,高亮输出,及其他特性。(推荐安装IPython)

启动Scrapy Shell

进入项目的根目录,执行下列命令来启动shell:

scrapy shell "http://www.itcast.cn/channel/teacher.shtml"

Scrapy Shell根据下载的页面会自动创建一些方便使用的对象,例如 Response 对象,以及 Selector 对象 (对HTML及XML内容)

  • 当shell载入后,将得到一个包含response数据的本地 response 变量,输入 response.body将输出response的包体,输出 response.headers 可以看到response的包头。

  • 输入 response.selector 时, 将获取到一个response 初始化的类 Selector 的对象,此时可以通过使用 response.selector.xpath()response.selector.css() 来对 response 进行查询。

  • Scrapy也提供了一些快捷方式, 例如 response.xpath()response.css()同样可以生效(如之前的案例)。

Selectors选择器

Scrapy Selectors 内置 XPath 和 CSS Selector 表达式机制

Selector有四个基本的方法,最常用的还是xpath:

  • xpath(): 传入xpath表达式,返回该表达式所对应的所有节点的selector list列表
  • extract(): 序列化该节点为Unicode字符串并返回list
  • css(): 传入CSS表达式,返回该表达式所对应的所有节点的selector list列表,语法同 BeautifulSoup4
  • re(): 根据传入的正则表达式对数据进行提取,返回Unicode字符串list列表

XPath表达式的例子及对应的含义:

/html/head/title: 选择<HTML>文档中 <head> 标签内的 <title> 元素
/html/head/title/text(): 选择上面提到的 <title> 元素的文字
//td: 选择所有的 <td> 元素
//div[@class="mine"]: 选择所有具有 class="mine" 属性的 div 元素

尝试Selector

我们用腾讯社招的网站http://hr.tencent.com/position.php?&start=0#a举例:

# 启动
scrapy shell "http://hr.tencent.com/position.php?&start=0#a" # 返回 xpath选择器对象列表
response.xpath('//title')
[<Selector xpath='//title' data=u'<title>\u804c\u4f4d\u641c\u7d22 | \u793e\u4f1a\u62db\u8058 | Tencent \u817e\u8baf\u62db\u8058</title'>] # 使用 extract()方法返回 Unicode字符串列表
response.xpath('//title').extract()
[u'<title>\u804c\u4f4d\u641c\u7d22 | \u793e\u4f1a\u62db\u8058 | Tencent \u817e\u8baf\u62db\u8058</title>'] # 打印列表第一个元素,终端编码格式显示
print response.xpath('//title').extract()[0]
<title>职位搜索 | 社会招聘 | Tencent 腾讯招聘</title> # 返回 xpath选择器对象列表
response.xpath('//title/text()')
<Selector xpath='//title/text()' data=u'\u804c\u4f4d\u641c\u7d22 | \u793e\u4f1a\u62db\u8058 | Tencent \u817e\u8baf\u62db\u8058'> # 返回列表第一个元素的Unicode字符串
response.xpath('//title/text()')[0].extract()
u'\u804c\u4f4d\u641c\u7d22 | \u793e\u4f1a\u62db\u8058 | Tencent \u817e\u8baf\u62db\u8058' # 按终端编码格式显示
print response.xpath('//title/text()')[0].extract()
职位搜索 | 社会招聘 | Tencent 腾讯招聘 response.xpath('//*[@class="even"]')
职位名称: print site[0].xpath('./td[1]/a/text()').extract()[0]
TEG15-运营开发工程师(深圳)
职位名称详情页: print site[0].xpath('./td[1]/a/@href').extract()[0]
position_detail.php?id=20744&keywords=&tid=0&lid=0
职位类别: print site[0].xpath('./td[2]/text()').extract()[0]
技术类

以后做数据提取的时候,可以把现在Scrapy Shell中测试,测试通过后再应用到代码中。

当然Scrapy Shell作用不仅仅如此,但是不属于我们课程重点,不做详细介绍。

官方文档:http://scrapy-chs.readthedocs.io/zh_CN/latest/topics/shell.html

最新文章

  1. JS函数的上下文环境
  2. !!!四种常见的 POST 提交数据方式(含application/json)
  3. Jquery 限制文本框输入字数【转】
  4. 【转】50条大牛C++编程开发学习建议
  5. [转载]【虚拟化系列】VMware vSphere 5.1 网络管理
  6. CTF中怎看phpinfo
  7. WEB前端面试真题 - 2000!大数的阶乘如何计算?
  8. 查看dmp文件
  9. No module named MySQLdb
  10. CF350E 【Wrong Floyd】
  11. Python 安装和 Pycharm 环境配置
  12. Charles几个常用测试功能小结
  13. pc远程控制凭证不工作的解决办法
  14. git教程: 创建版本库
  15. inotifywait实现目录监控--http://man.linuxde.net/inotifywait
  16. 20180831xlVBA_WorkbooksCosolidate
  17. failed to launch: nice -n 0 /home/hadoop/spark-2.3.3-bin-hadoop2.7/bin/spark-class org.apache.spark.deploy.worker.Worker --webui-port 8081 spark://namenode1:7077
  18. cnblogs第一篇文章
  19. JSP页面中文乱码
  20. 201621123006 《Java程序设计》第5周学习总结

热门文章

  1. Windows系统优化
  2. lua相关库安装常见问题
  3. oracle中 rownum 与 connect by的结合使用
  4. MySQL对指定字段进行加密(双向加密)
  5. module_init module_exit
  6. 转 Merkle Tree(默克尔树)算法解析
  7. Spring第八发—自动装配及让Spring自动扫描和管理Bean
  8. 测试CDockablePane。 测试他的最基本的功能。
  9. 关于/proc/进程idpid/fd ,根据fd来查找连接
  10. Thread was being aborted.