From a2b0737a1d77b12a8a2f07c3329d855e19df611d Mon Sep 17 00:00:00 2001 From: Pablo Hoffman Date: Sun, 7 Aug 2011 03:24:32 -0300 Subject: [PATCH] scrapy.utils.sitemap: added one more case of parsing invalid sitemaps --- scrapy/tests/test_utils_sitemap.py | 23 +++++++++++++++++++++++ scrapy/utils/sitemap.py | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/scrapy/tests/test_utils_sitemap.py b/scrapy/tests/test_utils_sitemap.py index 762ebe0ed..99b66feec 100644 --- a/scrapy/tests/test_utils_sitemap.py +++ b/scrapy/tests/test_utils_sitemap.py @@ -84,6 +84,29 @@ class SitemapTest(unittest.TestCase): {'loc': 'http://www.example.com/2', 'lastmod': ''}, ]) + def test_sitemap_wrong_ns2(self): + """We have seen sitemaps with wrongs ns. Presumably, Google still works + with these, though is not 100% confirmed""" + s = Sitemap(""" + + + http://www.example.com/ + 2009-08-16 + daily + 1 + + + http://www.example.com/2 + + + +""") + assert s.type == 'urlset' + self.assertEqual(list(s), + [{'priority': '1', 'loc': 'http://www.example.com/', 'lastmod': '2009-08-16', 'changefreq': 'daily'}, + {'loc': 'http://www.example.com/2', 'lastmod': ''}, + ]) + def test_sitemap_urls_from_robots(self): robots = """User-agent: * Disallow: /aff/ diff --git a/scrapy/utils/sitemap.py b/scrapy/utils/sitemap.py index 2a496f719..71d8122ab 100644 --- a/scrapy/utils/sitemap.py +++ b/scrapy/utils/sitemap.py @@ -16,7 +16,8 @@ class Sitemap(object): tree = ElementTree() tree.parse(StringIO(xmltext)) self._root = tree.getroot() - _, self.type = self._root.tag.split('}', 1) + rt = self._root.tag + self.type = self._root.tag.split('}', 1)[1] if '}' in rt else rt def __iter__(self): for elem in self._root.getchildren():