在Scrapy中启动shell来调试
来源:互联网 发布:决战武林天罡进阶数据 编辑:程序博客网 时间:2024/05/29 16:16
在Scrapy中写爬虫时,有时想在spider运行到某个位置时暂停,查看被处理的response, 以确认response是否是期望的。
这可以通过 scrapy.shell.inspect_response 函数来实现。
以下是如何在spider中调用该函数的例子:
import scrapyclass MySpider(scrapy.Spider): name = "myspider" start_urls = [ "http://example.com", "http://example.org", "http://example.net", ] def parse(self, response): # We want to inspect one specific response. if ".org" in response.url: from scrapy.shell import inspect_response inspect_response(response, self) # Rest of parsing code.
当运行spider时,您将得到类似下列的输出:
2017-10-23 17:48:31-0400 [myspider] DEBUG: Crawled (200) <GET http://example.com> (referer: None)2017-10-23 17:48:31-0400 [myspider] DEBUG: Crawled (200) <GET http://example.org> (referer: None)[s] Available Scrapy objects:[s] crawler <scrapy.crawler.Crawler object at 0x1e16b50>...>>> response.url'http://example.org'
接着测试提取代码:
>>> sel.xpath('//h1[@class="fn"]')[]
呃,看来是没有。您可以在浏览器里查看response的结果,判断是否是您期望的结果:
>>> view(response)True
最后您可以点击Ctrl-D(Windows下Ctrl-Z)来退出终端,恢复爬取:
>>> ^D2014-01-23 17:50:03-0400 [myspider] DEBUG: Crawled (200) <GET http://example.net> (referer: None)...
阅读全文
0 0
- 在Scrapy中启动shell来调试
- 用Scrapy shell调试xpath
- 用Scrapy shell调试xpath
- 在scrapy中使用django model来存储数据
- 如何在手机shell中通过命令行来启动我们的应用
- Scrapy shell调试返回403错误
- scrapy shell 调试返回403 返回为[]
- 启动Spark Shell,在Spark Shell中编写WordCount程序,在IDEA中编写WordCount的Maven程序,spark-submit使用spark的jar来做单词统计
- 在shell中设置调试函数
- 在chrome中启动xdebug调试
- Scrapy 在shell下抓取图片
- 为什么我在scrapy shell中没有看到hxs和xxs
- python 在 eclipse 中 导入 scrapy源码 进行调试,并安装 setuptools easy_install
- 在WinDBG中, 使用.shell命令来搜索字符串
- 在WinDBG中, 使用.shell命令来搜索字符串
- Scrapy Shell
- Scrapy Shell
- python利用Pycharm来运行调试Scrapy框架程序
- Android图片加载框架最全解析(六),探究Glide的自定义模块功能
- 积攒了这么多技术干货,总有一款适合你
- 快速集成微信登录。(超详细)
- 4.8 方法重载
- 计算机基础与java简介
- 在Scrapy中启动shell来调试
- java类加载机制
- Hudson 项目总结
- 安装软件注意事项
- Nginx反向代理,负载均衡,redis session共享,keepalived高可用
- win10专业版激活(试试水)
- Hadoop伪集群搭建
- 【广州云栖大数据/人工智能会场信息】全新一代人工智能引擎MaxCompute惊艳全场
- chrome performance 使用