From 240fdde6675a4a26518ab14d0e7c5a2208741eed Mon Sep 17 00:00:00 2001 From: Rolando Espinoza Date: Thu, 23 Jan 2014 11:08:56 -0400 Subject: [PATCH 1/4] Expose current crawler in the scrapy shell. --- scrapy/shell.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scrapy/shell.py b/scrapy/shell.py index 500c241f8..cd5fb677b 100644 --- a/scrapy/shell.py +++ b/scrapy/shell.py @@ -10,6 +10,7 @@ from twisted.internet import reactor, threads, defer from twisted.python import threadable from w3lib.url import any_to_uri +from scrapy.crawler import Crawler from scrapy.item import BaseItem from scrapy.spider import Spider from scrapy.selector import Selector @@ -24,7 +25,7 @@ from scrapy.exceptions import IgnoreRequest class Shell(object): - relevant_classes = (Spider, Request, Response, BaseItem, + relevant_classes = (Crawler, Spider, Request, Response, BaseItem, Selector, Settings) def __init__(self, crawler, update_vars=None, code=None): @@ -91,6 +92,7 @@ class Shell(object): self.populate_vars(response, request, spider) def populate_vars(self, response=None, request=None, spider=None): + self.vars['crawler'] = self.crawler self.vars['item'] = self.item_class() self.vars['settings'] = self.crawler.settings self.vars['spider'] = spider From 4081ba238dad2d048e29a336f26312e4da2eabff Mon Sep 17 00:00:00 2001 From: Rolando Espinoza Date: Thu, 23 Jan 2014 11:13:54 -0400 Subject: [PATCH 2/4] PEP8 minor edits. --- scrapy/shell.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/scrapy/shell.py b/scrapy/shell.py index cd5fb677b..6d81e0760 100644 --- a/scrapy/shell.py +++ b/scrapy/shell.py @@ -1,9 +1,10 @@ -""" -Scrapy Shell +"""Scrapy Shell See documentation in docs/topics/shell.rst + """ from __future__ import print_function + import signal from twisted.internet import reactor, threads, defer @@ -11,16 +12,16 @@ from twisted.python import threadable from w3lib.url import any_to_uri from scrapy.crawler import Crawler +from scrapy.exceptions import IgnoreRequest +from scrapy.http import Request, Response from scrapy.item import BaseItem -from scrapy.spider import Spider from scrapy.selector import Selector -from scrapy.utils.spider import create_spider_for_request +from scrapy.settings import Settings +from scrapy.spider import Spider +from scrapy.utils.console import start_python_console from scrapy.utils.misc import load_object from scrapy.utils.response import open_in_browser -from scrapy.utils.console import start_python_console -from scrapy.settings import Settings -from scrapy.http import Request, Response -from scrapy.exceptions import IgnoreRequest +from scrapy.utils.spider import create_spider_for_request class Shell(object): From 9aab9224cb1148b986764b26438c49bb96e4bb42 Mon Sep 17 00:00:00 2001 From: Rolando Espinoza Date: Thu, 23 Jan 2014 18:04:57 -0400 Subject: [PATCH 3/4] Updated shell docs with the crawler reference and fixed the actual shell output. Also updated the shell example with a reproducible code example. --- docs/intro/tutorial.rst | 18 +++++----- docs/topics/shell.rst | 73 ++++++++++++++++++++++++----------------- 2 files changed, 53 insertions(+), 38 deletions(-) diff --git a/docs/intro/tutorial.rst b/docs/intro/tutorial.rst index 246db1a60..bec653389 100644 --- a/docs/intro/tutorial.rst +++ b/docs/intro/tutorial.rst @@ -253,16 +253,18 @@ This is what the shell looks like:: [ ... Scrapy log here ... ] + 2014-01-23 17:11:42-0400 [default] DEBUG: Crawled (200) (referer: None) [s] Available Scrapy objects: - [s] 2010-08-19 21:45:59-0300 [default] INFO: Spider closed (finished) - [s] sel - [s] item Item() + [s] crawler + [s] item {} [s] request [s] response <200 http://www.dmoz.org/Computers/Programming/Languages/Python/Books/> - [s] spider + [s] sel \r\n\r\n + [s] settings + [s] spider [s] Useful shortcuts: - [s] shelp() Prints this help. - [s] fetch(req_or_url) Fetch a new request or URL and update objects + [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 >>> @@ -131,24 +134,27 @@ After that, we can star playing with the objects:: >>> fetch("http://slashdot.org") [s] Available Scrapy objects: - [s] sel - [s] item JobItem() + [s] crawler + [s] item {} [s] request [s] response <200 http://slashdot.org> - [s] settings - [s] spider + [s] sel \n\n\n\n\n\n