Scrapy, a fast high-level web crawling & scraping framework for Python.
improved changes to allow retrieval of alternate links in sitemaps, see #360
Stefan8ed2d0cda16c
diff --git a/docs/topics/spiders.rst b/docs/topics/spiders.rstindex 6586db668..e9afe1368 100644--- a/docs/topics/spiders.rst+++ b/docs/topics/spiders.rst@@ -560,6 +560,25 @@ SitemapSpiderBy default, all sitemaps are followed.+ .. attribute:: use_alternate_links++ Specifies if alternate links for one ``url`` should be followed. These+ are links for the same website in another language passed within+ the same ``url`` block.++ For example::++ <url>+ <loc>http://example.com/</loc>+ <xhtml:link rel="alternate" hreflang="de" href="http://example.com/de"/>+ </url>++ With ``use_alternate_links`` set, this would retrieve both URLs. With+ ``use_alternate_links`` disabled, only ``http://example.com/`` would be+ retrieved.++ Default is ``use_alternate_links`` disabled.+SitemapSpider examples~~~~~~~~~~~~~~~~~~~~~~diff --git a/scrapy/contrib/spiders/sitemap.py b/scrapy/contrib/spiders/sitemap.pyindex e4092c5d6..eb14b614d 100644--- a/scrapy/contrib/spiders/sitemap.py+++ b/scrapy/contrib/spiders/sitemap.py@@ -13,10 +13,6 @@ class SitemapSpider(BaseSpider):sitemap_follow = ['']def __init__(self, *a, **kw):- self._alternate = False- if 'alternate' in kw and kw.pop('alternate') == True:- self._alternate = True-super(SitemapSpider, self).__init__(*a, **kw)self._cbs = []for r, c in self.sitemap_rules:@@ -41,7 +37,7 @@ class SitemapSpider(BaseSpider):s = Sitemap(body)if s.type == 'sitemapindex':- for loc in iterloc(s, self._alternate):+ for loc in iterloc(s, self.use_alternate_links):if any(x.search(loc) for x in self._follow):yield Request(loc, callback=self._parse_sitemap)elif s.type == 'urlset':@@ -74,6 +70,6 @@ def iterloc(it, alt=False):yield d['loc']# Also consider alternate URLs (xhtml:link rel="alternate")- if alt == True and 'alternate' in d:+ if alt and 'alternate' in d:for l in d['alternate']:yield ldiff --git a/scrapy/tests/test_utils_sitemap.py b/scrapy/tests/test_utils_sitemap.pyindex a338adfe2..7423d1782 100644--- a/scrapy/tests/test_utils_sitemap.py+++ b/scrapy/tests/test_utils_sitemap.py@@ -171,6 +171,7 @@ Disallow: /forum/active/href="http://www.example.com/schweiz-deutsch/"/><xhtml:link rel="alternate" hreflang="en"href="http://www.example.com/english/"/>+ <xhtml:link rel="alternate" hreflang="en"/><!-- wrong tag without href --></url></urlset>""")diff --git a/scrapy/utils/sitemap.py b/scrapy/utils/sitemap.pyindex b69b4226f..24a540531 100644--- a/scrapy/utils/sitemap.py+++ b/scrapy/utils/sitemap.py@@ -12,7 +12,7 @@ class Sitemap(object):(type=sitemapindex) files"""def __init__(self, xmltext):- xmlp = lxml.etree.XMLParser(recover=True)+ xmlp = lxml.etree.XMLParser(recover=True, remove_comments=True)self._root = lxml.etree.fromstring(xmltext, parser=xmlp)rt = self._root.tagself.type = self._root.tag.split('}', 1)[1] if '}' in rt else rt@@ -25,10 +25,11 @@ class Sitemap(object):name = tag.split('}', 1)[1] if '}' in tag else tagif name == 'link':- d.setdefault('alternate', []).append(el.get('href'))+ if 'href' in el.attrib:+ d.setdefault('alternate', []).append(el.get('href'))else:d[name] = el.text.strip() if el.text else ''-+if 'loc' in d:yield d