Fix XmlTagUrlParser and make Python 3 compatible

URLs within a sitemap file were not being captured.
This commit is contained in:
Chris Mayo 2019-10-28 19:19:25 +00:00
parent 8bdd402aed
commit ec8b6e09f0
3 changed files with 21 additions and 4 deletions

View file

@ -29,7 +29,10 @@ class XmlTagUrlParser(object):
self.tag = tag
self.parser = ParserCreate()
self.parser.buffer_text = True
self.parser.returns_unicode = True
try:
self.parser.returns_unicode = True
except AttributeError:
pass # Python 3
self.parser.StartElementHandler = self.start_element
self.parser.EndElementHandler = self.end_element
self.parser.CharacterDataHandler = self.char_data
@ -37,7 +40,7 @@ class XmlTagUrlParser(object):
def parse(self, url_data):
"""Parse XML URL data."""
self.url_data = url_data
self.loc = False
self.in_tag = False
self.url = u""
data = url_data.get_raw_content()
isfinal = True
@ -45,6 +48,7 @@ class XmlTagUrlParser(object):
self.parser.Parse(data, isfinal)
except ExpatError as expaterr:
self.url_data.add_warning(expaterr.message,tag=WARN_XML_PARSE_ERROR)
def start_element(self, name, attrs):
"""Set tag status for start element."""
self.in_tag = (name == self.tag)
@ -65,7 +69,7 @@ class XmlTagUrlParser(object):
def char_data(self, data):
"""If inside the wanted tag, append data to URL."""
if self.loc:
if self.in_tag:
self.url += data
@ -77,4 +81,3 @@ def parse_sitemap(url_data):
def parse_sitemapindex(url_data):
"""Parse XML sitemap index data."""
XmlTagUrlParser(u"loc").parse(url_data)

View file

@ -6,4 +6,10 @@
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>http://www.example.com/?ascii=nø</loc>
<lastmod>2005-01-01</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>

View file

@ -2,3 +2,11 @@ url http://localhost:%(port)d/%(datadir)s/sitemap.xml
cache key http://localhost:%(port)d/%(datadir)s/sitemap.xml
real url http://localhost:%(port)d/%(datadir)s/sitemap.xml
valid
url http://www.example.com/
cache key http://www.example.com/
real url http://www.example.com/
valid
url http://www.example.com/?ascii=nø
cache key http://www.example.com/?ascii=n%%C3%%B8
real url http://www.example.com/?ascii=n%%C3%%B8
valid