Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4248286
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:13:57+00:00 2026-05-21T04:13:57+00:00

I am trying to parse sitemap.xml files using scrapy, the sitemap files are like

  • 0

I am trying to parse sitemap.xml files using scrapy, the sitemap files are like the following one with just much more url nodes.

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:video="http://www.sitemaps.org/schemas/sitemap-video/1.1">
    <url>
        <loc>
            http://www.site.com/page.html
        </loc>
        <video:video>
            <video:thumbnail_loc>
                http://www.site.com/thumb.jpg
            </video:thumbnail_loc>
            <video:content_loc>http://www.example.com/video123.flv</video:content_loc>
            <video:player_loc allow_embed="yes" autoplay="ap=1">
                http://www.example.com/videoplayer.swf?video=123
            </video:player_loc>
            <video:title>here is the page title</video:title>
            <video:description>and an awesome description</video:description>
            <video:duration>302</video:duration>
            <video:publication_date>2011-02-24T02:03:43+02:00</video:publication_date>
            <video:tag>w00t</video:tag>
            <video:tag>awesome</video:tag>
            <video:tag>omgwtfbbq</video:tag>
            <video:tag>kthxby</video:tag>
        </video:video>
    </url>
</urlset>

I looked at the related scrapy’s documentation, and i wrote the following snippet to see if i was doing the right way (and it seems i don’t ^^):

class SitemapSpider(XMLFeedSpider):
    name = "sitemap"
    namespaces = [
        ('', 'http://www.sitemaps.org/schemas/sitemap/0.9'),
        ('video', 'http://www.sitemaps.org/schemas/sitemap-video/1.1'),
    ]
    start_urls = ["http://example.com/sitemap.xml"]
    itertag = 'url'

    def parse_node(self, response, node):
        print "Parsing: %s" % str(node)

But when i run the spider, i get this error:

File "/.../python2.7/site-packages/scrapy/utils/iterators.py", line 32, in xmliter
    yield XmlXPathSelector(text=nodetext).select('//' + nodename)[0]
    exceptions.IndexError: list index out of range

I think i’m not defining the “default” namespace (http://www.sitemaps.org/schemas/sitemap/0.9) properly, but i can’t find how to do this.

What’s the correct way to iterate over the url nodes and then be able to extract the needed infos from its childs?


ANSWER:

Unfortunately, i wasn’t able to use the XMLFeedSpider (which is supposed to be the way to parse XML with scrapy), but thanks to simplebias’ answer, i have been able to figure a way to achieve this “the old-school way”. I came up with the following code (which works, this time!):

class SitemapSpider(BaseSpider):
    name = 'sitemap'
    namespaces = {
        'sitemap': 'http://www.sitemaps.org/schemas/sitemap/0.9',
        'video': 'http://www.sitemaps.org/schemas/sitemap-video/1.1',
    }

    def parse(self, response):
        xxs = XmlXPathSelector(response)
        for namespace, schema in self.namespaces.iteritems():
            xxs.register_namespace(namespace, schema)
        for urlnode in xxs.select('//sitemap:url'):
            extract_datas_here()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:14:00+00:00Added an answer on May 21, 2026 at 4:14 am

    Scrapy uses lxml / libxml2 under the hood, eventually invoking the node.xpath() method to perform the selection. Any elements in your xpath expression which are namespaced must be prefixed, and you must pass a mapping to tell the selector which namespace each prefix resolves to.

    Here is an example to illustrate how to map prefixes to namespaces when using the node.xpath() method:

    doc = '<root xmlns="chaos"><bar /></root>'
    tree = lxml.etree.fromstring(doc)
    tree.xpath('//bar')
    []
    tree.xpath('//x:bar', namespaces={'x': 'chaos'})
    [<Element {chaos}bar at 7fa40f9c50a8>]
    

    Without having used this scrapy XMLFeedSpider class, I’m guessing your namespace map and itertag need to follow the same scheme:

    class SitemapSpider(XMLFeedSpider):
        namespaces = [
            ('sm', 'http://www.sitemaps.org/schemas/sitemap/0.9'),
            ]
         itertag = 'sm:url'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse XML files and I would like to provide a schema
I am trying to use JQuery to parse a sitemap.xml to look like this
I trying to parse the message element out of the following XML fragment using
Trying to parse some XML but apparently this is too much for a lazy
I have an xml feed at this url Now im trying parse the content,
I'm trying to parse some Android XML using XmlSlurper . For a given child
I am trying to parse XML using PHP DOM and then insert this data
Im trying to parse a Xml file from a URL, the xml is actually
I trying to parse some xml in Google Refine using Jython and ElementTree but
Im trying to parse the following XML file(generated on iphone with KISSxml) with KissXML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.