using a method definition instead of a None attribute

This commit is contained in:
Victor Torres 2018-11-29 18:33:20 -03:00
parent a5e1b7bb47
commit 672385a371
2 changed files with 11 additions and 7 deletions

View File

@ -680,7 +680,7 @@ SitemapSpider
Default is ``sitemap_alternate_links`` disabled.
.. attribute:: sitemap_filter
.. method:: sitemap_filter(urls)
Specifies a function to filter sitemap entries and their attributes.
@ -703,7 +703,7 @@ SitemapSpider
This would retrieve only ``urls`` modified on 2005 and the following
years.
If you omit this attribute, all urls found in sitemaps will be
If you omit this method, all urls found in sitemaps will be
processed, observing other attributes and their settings.

View File

@ -17,7 +17,6 @@ class SitemapSpider(Spider):
sitemap_rules = [('', 'parse')]
sitemap_follow = ['']
sitemap_alternate_links = False
sitemap_filter = None
def __init__(self, *a, **kw):
super(SitemapSpider, self).__init__(*a, **kw)
@ -32,6 +31,14 @@ class SitemapSpider(Spider):
for url in self.sitemap_urls:
yield Request(url, self._parse_sitemap)
def sitemap_filter(self, urls):
"""This method can be used to filter sitemap entries by their
attributes, for example, you can filter locs with lastmod greater
than a given date (see docs).
"""
for url in urls:
yield url
def _parse_sitemap(self, response):
if response.url.endswith('/robots.txt'):
for url in sitemap_urls_from_robots(response.text, base_url=response.url):
@ -44,10 +51,7 @@ class SitemapSpider(Spider):
return
s = Sitemap(body)
if callable(self.sitemap_filter):
it = self.sitemap_filter(s)
else:
it = s
it = self.sitemap_filter(s)
if s.type == 'sitemapindex':
for loc in iterloc(it, self.sitemap_alternate_links):