1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
|
======= ===================
SEP 18
Title Spider Middleware v2
Author Insophia Team
Created 2010-06-20
Status Draft (in progress)
======= ===================
=============================
SEP-018: Spider middleware v2
=============================
This SEP introduces a new architecture for spider middlewares which provides a
greater degree of modularity to combine functionality which can be plugged in
from different (reusable) middlewares.
The purpose of !SpiderMiddleware-v2 is to define an architecture that
encourages more re-usability for building spiders based on smaller well-tested
components. Those components can be global (similar to current spider
middlewares) or per-spider that can be combined to achieve the desired
functionality. These reusable components will benefit all Scrapy users by
building a repository of well-tested components that can be shared among
different spiders and projects. Some of them will come bundled with Scrapy.
Unless explicitly stated, in this document "spider middleware" refers to the
**new** spider middleware v2, not the old one.
This document is a work in progress, see `Pending Issues`_ below.
New spider middleware API
=========================
A spider middleware can implement any of the following methods:
- ``process_response(response, request, spider)``
- Process a (downloaded) response
- Receives: The ``response`` to process, the ``request`` used to download
the response (not necessarily the request sent from the spider), and the
``spider`` that requested it.
- Returns: A list containing requests and/or items
- ``process_error(error, request, spider)``:
- Process a error when trying to download a request, such as DNS errors,
timeout errors, etc.
- Receives: The ``error`` caused, the ``request`` that caused it (not
necessarily the request sent from the spider), and then ``spider`` that
requested it.
- Returns: A list containing request and/or items
- ``process_request(request, response, spider)``
- Process a request after it has been extracted from the spider or previous
middleware ``process_response()`` methods.
- Receives: The ``request`` to process, the ``response`` where the request
was extracted from, and the ``spider`` that extracted it.
- Note: ``response`` is ``None`` for start requests, or requests injected
directly (through ``manager.scraper.process_request()`` without
specifying a response (see below)
- Returns: A ``Request`` object (not necessarily the same received), or
``None`` in which case the request is dropped.
- ``process_item(item, response, spider)``
- Process an item after it has been extracted from the spider or previous
middleware ``process_response()`` methods.
- Receives: The ``item`` to process, the ``response`` where the item was
extracted from, and the ``spider`` that extracted it.
- Returns: An ``Item`` object (not necessarily the same received), or
``None`` in which case the item is dropped.
- ``next_request(spider)``
- Returns a the next request to crawl with this spider. This method is
called when the spider is opened, and when it gets idle.
- Receives: The ``spider`` to return the next request for.
- Returns: A ``Request`` object.
- ``open_spider(spider)``
- This can be used to allocate resources when a spider is opened.
- Receives: The ``spider`` that has been opened.
- Returns: nothing
- ``close_spider(spider)``
- This can be used to free resources when a spider is closed.
- Receives: The ``spider`` that has been closed.
- Returns: nothing
Changes to core API
===================
Injecting requests to crawl
---------------------------
To inject start requests (or new requests without a response) to crawl, you used before:
- ``manager.engine.crawl(request, spider)``
Now you'll use:
- ``manager.scraper.process_request(request, spider, response=None)``
Which (unlike the old ``engine.crawl`` will make the requests pass through the spider middleware ``process_request()`` method).
Scheduler middleware to be removed
----------------------------------
We're gonna remove the Scheduler Middleware, and move the duplicates filter to a new spider middleware.
Scraper high-level API
======================
There is a simpler high-level API - the Scraper API - which is the API used by the engine and other core components. This is also the API implemented by this new middleware, with its own internal architecture and hooks. Here is the Scraper API:
- ``process_response(response, request, spider)``
- returns iterable of items and requests
- ``process_error(error, request, spider)``
- returns iterable of items and requests
- ``process_request(request, spider, response=None)``
- injects a request to crawl for the given spider
- ``process_item(item, spider, response)``
- injects a item to process with the item processor (typically the item
pipeline)
- ``next_request(spider)``
- returns the next request to process for the given spider
- ``open_spider(spider)``
- opens a spider
- ``close_spider(spider)``
- closes a spider
How it works
================
The spider middlewares are defined in certain order with the top-most being the
one closer to the engine, and the bottom-most being the one closed to the
spider.
Example:
- Engine
- Global spider Middleware 3
- Global spider Middleware 2
- Global spider Middleware 1
- Spider-specific middlewares (defined in ``Spider.middlewares``)
- Spider-specific middleware 3
- Spider-specific middleware 2
- Spider-specific middleware 1
- Spider
The data flow with Spider Middleware v2 is as follows:
1. When a response arrives from the engine, it is passed through all the spider
middlewares (in descending order). The result of each middleware
``process_response`` is kept and then returned along with the spider
callback result
2. Each item of the aggregated result from previous point is passed through all
middlewares (in ascending order) calling the ``process_request`` or
``process_item`` method accordingly, and their results are kept for passing
to the following middlewares
One of the spider middlewares (typically - but not necessarily - the last
spider middleware closer to the spider, as shown in the example) will be a
"spider-specific spider middleware" which would take care of calling the
additional spider middlewares defined in the ``Spider.middlewares`` attribute,
hence providing support for per-spider middlewares. If the middleware is well
written, it should work both globally and per-spider.
Spider-specific middlewares
===========================
You can define in the spider itself a list of additional middlewares that will
be used for this spider, and only this spider. If the middleware is well
written, it should work both globally and per spider.
Here's an example that combines functionality from multiple middlewares into
the same spider:
.. code-block:: python
#!python
class MySpider(BaseSpider):
middlewares = [
RegexLinkExtractor(),
CallbackRules(),
CanonicalizeUrl(),
ItemIdSetter(),
OffsiteMiddleware(),
]
allowed_domains = ["example.com", "sub.example.com"]
url_regexes_to_follow = ["/product.php?.*"]
callback_rules = {
"/product.php.*": "parse_product",
"/category.php.*": "parse_category",
}
canonicalization_rules = ["sort-query-args", "normalize-percent-encoding", ...]
id_field = "guid"
id_fields_to_hash = ["supplier_name", "supplier_id"]
def parse_product(self, item):
# extract item from response
return item
def parse_category(self, item):
# extract item from response
return item
The Spider Middleware that implements spider code
=================================================
There's gonna be one middleware that will take care of calling the proper
spider methods on each event such as:
- call ``Request.callback`` (for 200 responses) or ``Request.errback`` for
non-200 responses and other errors. this behaviour can be changed through the
``handle_httpstatus_list`` spider attribute.
- if ``Request.callback`` is not set it will use ``Spider.parse``
- if ``Request.errback`` is not set it will use ``Spider.errback``
- call additional spider middlewares defined in the ``Spider.middlewares``
attribute
- call ``Spider.next_request()`` and ``Spider.start_requests()`` on
``next_request()`` middleware method (this would implicitly support backward
compatibility)
Differences with Spider middleware v1
=====================================
- adds support for per-spider middlewares through the ``Spider.middlewares``
attribute
- allows processing initial requests (those returned from
``Spider.start_requests()``)
Use cases and examples
======================
This section contains several examples and use cases for Spider Middlewares.
Imports are intentionally removed for conciseness and clarity.
Regex (HTML) Link Extractor
---------------------------
A typical application of spider middlewares could be to build Link Extractors.
For example:
.. code-block:: python
#!python
class RegexHtmlLinkExtractor(object):
def process_response(self, response, request, spider):
if isinstance(response, HtmlResponse):
allowed_regexes = spider.url_regexes_to_follow
# extract urls to follow using allowed_regexes
return [Request(x) for x in urls_to_follow]
# Example spider using this middleware
class MySpider(BaseSpider):
middlewares = [RegexHtmlLinkExtractor()]
url_regexes_to_follow = ["/product.php?.*"]
# parsing callbacks below
RSS2 link extractor
-------------------
.. code-block:: python
#!python
class Rss2LinkExtractor(object):
def process_response(self, response, request, spider):
if response.headers.get("Content-type") == "application/rss+xml":
xs = XmlXPathSelector(response)
urls = xs.select("//item/link/text()").extract()
return [Request(x) for x in urls]
Callback dispatcher based on rules
----------------------------------
Another example could be to build a callback dispatcher based on rules:
.. code-block:: python
#!python
class CallbackRules(object):
def __init__(self):
self.rules = {}
dispatcher.connect(signals.spider_opened, self.spider_opened)
dispatcher.connect(signals.spider_closed, self.spider_closed)
def spider_opened(self, spider):
self.rules[spider] = {}
for regex, method_name in spider.callback_rules.items():
r = re.compile(regex)
m = getattr(self.spider, method_name, None)
if m:
self.rules[spider][r] = m
def spider_closed(self, spider):
del self.rules[spider]
def process_response(self, response, request, spider):
for regex, method in self.rules[spider].items():
m = regex.search(response.url)
if m:
return method(response)
return []
# Example spider using this middleware
class MySpider(BaseSpider):
middlewares = [CallbackRules()]
callback_rules = {
"/product.php.*": "parse_product",
"/category.php.*": "parse_category",
}
def parse_product(self, response):
# parse response and populate item
return item
URL Canonicalizers
------------------
Another example could be for building URL canonicalizers:
.. code-block:: python
#!python
class CanonicalizeUrl(object):
def process_request(self, request, response, spider):
curl = canonicalize_url(request.url, rules=spider.canonicalization_rules)
return request.replace(url=curl)
# Example spider using this middleware
class MySpider(BaseSpider):
middlewares = [CanonicalizeUrl()]
canonicalization_rules = ["sort-query-args", "normalize-percent-encoding", ...]
# ...
Setting item identifier
-----------------------
Another example could be for setting a unique identifier to items, based on
certain fields:
.. code-block:: python
#!python
class ItemIdSetter(object):
def process_item(self, item, response, spider):
id_field = spider.id_field
id_fields_to_hash = spider.id_fields_to_hash
item[id_field] = make_hash_based_on_fields(item, id_fields_to_hash)
return item
# Example spider using this middleware
class MySpider(BaseSpider):
middlewares = [ItemIdSetter()]
id_field = "guid"
id_fields_to_hash = ["supplier_name", "supplier_id"]
def parse(self, response):
# extract item from response
return item
robots.txt exclusion
--------------------
A spider middleware to avoid visiting pages forbidden by robots.txt:
.. code-block:: python
#!python
class SpiderInfo(object):
def __init__(self, useragent):
self.useragent = useragent
self.parsers = {}
self.pending = defaultdict(list)
class AllowAllParser(object):
def can_fetch(useragent, url):
return True
class RobotsTxtMiddleware(object):
REQUEST_PRIORITY = 1000
def __init__(self):
self.spiders = {}
dispatcher.connect(self.spider_opened, signal=signals.spider_opened)
dispatcher.connect(self.spider_closed, signal=signals.spider_closed)
def process_request(self, request, response, spider):
return self.process_start_request(request)
def process_start_request(self, request, spider):
info = self.spiders[spider]
url = urlparse_cached(request)
netloc = url.netloc
if netloc in info.parsers:
rp = info.parsers[netloc]
if rp.can_fetch(info.useragent, request.url):
res = request
else:
spider.log("Forbidden by robots.txt: %s" % request)
res = None
else:
if netloc in info.pending:
res = None
else:
robotsurl = "%s://%s/robots.txt" % (url.scheme, netloc)
meta = {"spider": spider, "handle_httpstatus_list": [403, 404, 500]}
res = Request(
robotsurl,
callback=self.parse_robots,
meta=meta,
priority=self.REQUEST_PRIORITY,
)
info.pending[netloc].append(request)
return res
def parse_robots(self, response):
spider = response.request.meta["spider"]
netloc = urlparse_cached(response).netloc
info = self.spiders[spider]
if response.status == 200:
rp = robotparser.RobotFileParser(response.url)
rp.parse(response.body.splitlines())
info.parsers[netloc] = rp
else:
info.parsers[netloc] = AllowAllParser()
return info.pending[netloc]
def spider_opened(self, spider):
ua = getattr(spider, "user_agent", None) or settings["USER_AGENT"]
self.spiders[spider] = SpiderInfo(ua)
def spider_closed(self, spider):
del self.spiders[spider]
Offsite middleware
------------------
This is a port of the Offsite middleware to the new spider middleware API:
.. code-block:: python
#!python
class SpiderInfo(object):
def __init__(self, host_regex):
self.host_regex = host_regex
self.hosts_seen = set()
class OffsiteMiddleware(object):
def __init__(self):
self.spiders = {}
dispatcher.connect(self.spider_opened, signal=signals.spider_opened)
dispatcher.connect(self.spider_closed, signal=signals.spider_closed)
def process_request(self, request, response, spider):
return self.process_start_request(self, request)
def process_start_request(self, request, spider):
if self.should_follow(request, spider):
return request
else:
info = self.spiders[spider]
host = urlparse_cached(x).hostname
if host and host not in info.hosts_seen:
spider.log("Filtered offsite request to %r: %s" % (host, request))
info.hosts_seen.add(host)
def should_follow(self, request, spider):
info = self.spiders[spider]
# hostname can be None for wrong urls (like javascript links)
host = urlparse_cached(request).hostname or ""
return bool(info.regex.search(host))
def get_host_regex(self, spider):
"""Override this method to implement a different offsite policy"""
domains = [d.replace(".", r"\.") for d in spider.allowed_domains]
regex = r"^(.*\.)?(%s)$" % "|".join(domains)
return re.compile(regex)
def spider_opened(self, spider):
info = SpiderInfo(self.get_host_regex(spider))
self.spiders[spider] = info
def spider_closed(self, spider):
del self.spiders[spider]
Limit URL length
----------------
A middleware to filter out requests with long urls:
.. code-block:: python
#!python
class LimitUrlLength(object):
def __init__(self):
self.maxlength = settings.getint("URLLENGTH_LIMIT")
def process_request(self, request, response, spider):
return self.process_start_request(self, request)
def process_start_request(self, request, spider):
if len(request.url) <= self.maxlength:
return request
spider.log(
"Ignoring request (url length > %d): %s " % (self.maxlength, request.url)
)
Set Referer
-----------
A middleware to set the Referer:
.. code-block:: python
#!python
class SetReferer(object):
def process_request(self, request, response, spider):
request.headers.setdefault("Referer", response.url)
return request
Set and limit crawling depth
----------------------------
A middleware to set (and limit) the request/response depth, taken from the
start requests:
.. code-block:: python
#!python
class SetLimitDepth(object):
def __init__(self, maxdepth=0):
self.maxdepth = maxdepth or settings.getint("DEPTH_LIMIT")
def process_request(self, request, response, spider):
depth = response.request.meta["depth"] + 1
request.meta["depth"] = depth
if not self.maxdepth or depth <= self.maxdepth:
return request
spider.log("Ignoring link (depth > %d): %s " % (self.maxdepth, request))
def process_start_request(self, request, spider):
request.meta["depth"] = 0
return request
Filter duplicate requests
-------------------------
A middleware to filter out requests already seen:
.. code-block:: python
#!python
class FilterDuplicates(object):
def __init__(self):
clspath = settings.get("DUPEFILTER_CLASS")
self.dupefilter = load_object(clspath)()
dispatcher.connect(self.spider_opened, signal=signals.spider_opened)
dispatcher.connect(self.spider_closed, signal=signals.spider_closed)
def enqueue_request(self, spider, request):
seen = self.dupefilter.request_seen(spider, request)
if not seen or request.dont_filter:
return request
def spider_opened(self, spider):
self.dupefilter.open_spider(spider)
def spider_closed(self, spider):
self.dupefilter.close_spider(spider)
Scrape data using Parsley
-------------------------
A middleware to Scrape data using Parsley as described in UsingParsley
.. code-block:: python
#!python
from pyparsley import PyParsley
class ParsleyExtractor(object):
def __init__(self, parsley_json_code):
parsley = json.loads(parselet_json_code)
class ParsleyItem(Item):
def __init__(self, *a, **kw):
for name in parsley.keys():
self.fields[name] = Field()
super(ParsleyItem, self).__init__(*a, **kw)
self.item_class = ParsleyItem
self.parsley = PyParsley(parsley, output="python")
def process_response(self, response, request, spider):
return self.item_class(self.parsley.parse(string=response.body))
Pending issues
==============
Resolved:
- how to make ``start_requests()`` output pass through spider middleware
``process_request()``?
- Start requests will be injected through
``manager.scraper.process_request()`` instead of
``manager.engine.crawl()``
- should we support adding additional start requests from a spider middleware?
- Yes - there is a spider middleware method (``start_requests()``) for that
- should ``process_response()`` receive a ``request`` argument with the
``request`` that originated it?. ``response.request`` is the latest request,
not the original one (think of redirections), but it does carry the ``meta``
of the original one. The original one may not be available anymore (in
memory) if we're using a persistent scheduler., but in that case it would be
the deserialized request from the persistent scheduler queue.
- No - this would make implementation more complex and we're not sure it's
really needed
- how to make sure ``Request.errback`` is always called if there is a problem
with the request?. Do we need to ensure that?. Requests filtered out (by
returning ``None``) in the ``process_request()`` method will never be
callback-ed or even errback-ed. this could be a problem for spiders that want
to be notified if their requests are dropped. should we support this
notification somehow or document (the lack of) it properly?
- We won't support notifications of dropped requests, because: 1. it's hard
to implement and unreliable, 2. it's against not friendly with request
persistence, 3. we can't come up with a good api.
- should we make the list of default spider middlewares empty? (or the
"per-spider" spider middleware alone)
- No - there are some useful spider middlewares that it's worth enabling by
default like referer, duplicates, robots2
- should we allow returning deferreds in spider middleware methods?
- Yes - we should build a Deferred with the spider middleware methods as
callbacks and that would implicitly support returning Deferreds
- should we support processing responses before they're processed by the
spider, because ``process_response`` runs "in parallel" to the spider
callback, and can't stop from running it.
- No - we haven't seen a practical use case for this, so we won't add an
additional hook. It should be trivial to add it later, if needed.
- should we make a spider middleware to handle calling the request and spider
callback, instead of letting the Scraper component do it?
- Yes - there's gonna a spider middleware for execution spider-specific code
such as callbacks and also custom middlewares
|