Python:Scrapy Shell的使用教程
2014-02-20 16:38
591 查看
Scrapy shell 是一个交互式的shell,一旦你习惯使用了Scrapy shell,你将会发现Scrapy shell对于开发爬虫是非常好用的一个测试工具。
在使用Scrapy shell之前,你需要先安装ipython(可以在http://www.lfd.uci.edu/~gohlke/pythonlibs/查找相应版本的ipython进行安装)。
fetch(request_or_url)-从给定的url或既有的request请求对象重新生成response对象,并更新原有的相关对象
view(response)-使用浏览器打开原有的response对象(换句话说就是html页面)
这些可用的对象有:
crawler - 当前的Crawler对象
spider
request - 最后获取页面的请求对象
response - 一个包含最后获取页面的响应对象
sel - 最新下载页面的Selector对象
settings - 当前的Scrapy settings
首先,我们启动shell
以上命令执行后,会使用Scrapy downloader下载指定url的页面数据,并且打印出可用的对象和函数列表
这个需求可以通过scrapy.shell.inspect_response函数进行实现
以下是一个关于如何从spider中调用scrapy shell的例子
注意:当Scrapy engine被scrapy shell占用的时候,Scrapy shell中的fetch函数是无法使用的。 然而,当你退出Scrapy shell的时候,蜘蛛将从停止的地方继续爬行
作者:曾是土木人(http://blog.csdn.net/php_fly)
原文地址:/article/1611878.html
参考文章:Scrapy shell
在使用Scrapy shell之前,你需要先安装ipython(可以在http://www.lfd.uci.edu/~gohlke/pythonlibs/查找相应版本的ipython进行安装)。
启用shell
可以使用如下命令启用shellscrapy shell <url>其中<url>就是你想抓取的页面url
使用shell
Scrapy shell可以看成是一个内置了几个有用的功能函数的python控制台程序。功能函数
shelp() - 输出一系列可用的对象和函数fetch(request_or_url)-从给定的url或既有的request请求对象重新生成response对象,并更新原有的相关对象
view(response)-使用浏览器打开原有的response对象(换句话说就是html页面)
Scrapy 对象
使用Scrapy shell下载指定页面的时候,会生成一些可用的对象,比如Response对象和Selector对象(Html和XML均适用)这些可用的对象有:
crawler - 当前的Crawler对象
spider
request - 最后获取页面的请求对象
response - 一个包含最后获取页面的响应对象
sel - 最新下载页面的Selector对象
settings - 当前的Scrapy settings
Scrapy shell例子
以我的个人博客作为测试:http://blog.csdn.net/php_fly首先,我们启动shell
scrapy shell http://blog.csdn.net/php_fly --nolog
以上命令执行后,会使用Scrapy downloader下载指定url的页面数据,并且打印出可用的对象和函数列表
[s] Available Scrapy objects: [s] crawler <scrapy.crawler.Crawler object at 0x0000000002AEF7B8> [s] item {} [s] request <GET http://blog.csdn.net/php_fly> [s] response <200 http://blog.csdn.net/php_fly> [s] sel <Selector xpath=None data=u'<html xmlns="http://www.w3.org/1999/xhtm'> [s] settings <CrawlerSettings module=None> [s] spider <Spider 'default' at 0x4cdb940> [s] Useful shortcuts: [s] shelp() Shell help (print this help) [s] fetch(req_or_url) Fetch request (or URL) and update local objects [s] view(response) View response in a browser
获取曾是土木人博客的文章列表超链接
In [9]: sel.xpath("//span[@class='link_title']/a/@href").extract() Out[9]: [u'/php_fly/article/details/19364913', u'/php_fly/article/details/18155421', u'/php_fly/article/details/17629021', u'/php_fly/article/details/17619689', u'/php_fly/article/details/17386163', u'/php_fly/article/details/17266889', u'/php_fly/article/details/17172381', u'/php_fly/article/details/17171985', u'/php_fly/article/details/17145295', u'/php_fly/article/details/17122961', u'/php_fly/article/details/17117891', u'/php_fly/article/details/14533681', u'/php_fly/article/details/13162011', u'/php_fly/article/details/12658277', u'/php_fly/article/details/12528391', u'/php_fly/article/details/12421473', u'/php_fly/article/details/12319943', u'/php_fly/article/details/12293587', u'/php_fly/article/details/12293381', u'/php_fly/article/details/12289803']
修改scrapy shell的请求方式:
>>> request = request.replace(method="POST") >>> fetch(request) [s] Available Scrapy objects: [s] crawler <scrapy.crawler.Crawler object at 0x1e16b50> ...
从Spider中调用Scrapy shell
在爬虫运行过程中,有时需要检查某个响应是否是你所期望的。这个需求可以通过scrapy.shell.inspect_response函数进行实现
以下是一个关于如何从spider中调用scrapy shell的例子
from scrapy.spider import Spider class MySpider(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) # Rest of parsing code.当你启动爬虫的时候,控制台将打印出类似如下的信息
2014-02-20 17:48:31-0400 [myspider] DEBUG: Crawled (200) <GET http://example.com> (referer: None) 2014-02-20 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'
注意:当Scrapy engine被scrapy shell占用的时候,Scrapy shell中的fetch函数是无法使用的。 然而,当你退出Scrapy shell的时候,蜘蛛将从停止的地方继续爬行
作者:曾是土木人(http://blog.csdn.net/php_fly)
原文地址:/article/1611878.html
参考文章:Scrapy shell
相关文章推荐
- Scrapy Shell的使用教程
- Python:Scrapy框架中Item Pipeline组件(项目管道组件)的使用教程
- Python的Crypto模块使用:自动输入Shell中的密码
- Python scrapy基础教程(三)
- 在Python程序中操作文件之flush()方法的使用教程
- 在Linux下使用Python的matplotlib绘制数据图的教程
- 在Python中操作文件之truncate()方法的使用教程
- 【python中调用shell命令使用PIPE】使用PIPE作为stdout出现假卡死的情况——将stdout重定向为输出到临时文件
- 教程:使用QT建立Python GUI应用程序
- 如何在Windows下开发Python:在cmd下运行Python脚本+如何使用Python Shell(command line模式和GUI模式)+如何使用Python IDE
- TPYBoard MicroPython 固件开发教程使用mpy-cross工具生成mpy文件
- Python网络爬虫(八):Scrapy框架的使用
- 在Python中使用HTML模版的教程
- 针对小白的Python入门教程——Python使用说明书
- python学习之Mac安装Scrapy指南教程
- 在Python中使用PIL模块处理图像的教程
- Python Qt5 Creator 使用创建项目教程
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程
- 零基础写python爬虫之使用Scrapy框架编写爬虫
- redhat下使用mysql,python,redis,httpsqs,shell脚本开机自启动的一些问题与解决