2

我正在尝试sitemap.xml使用scrapy解析文件,站点地图文件类似于以下一个只有更多url节点的文件。

<?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>

我查看了相关的scrapy文档,并编写了以下代码片段以查看我是否以正确的方式进行操作(看来我没有^^):

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)

但是当我运行蜘蛛时,我得到了这个错误:

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

我想我没有正确定义“默认”命名空间(http://www.sitemaps.org/schemas/sitemap/0.9),但我找不到如何做到这一点。

迭代url节点然后能够从其子节点中提取所需信息的正确方法是什么?


回答:

不幸的是,我无法使用XMLFeedSpider(这应该是解析 XML 的方式scrapy),但由于 simplebias 的回答,我已经能够想出一种方法来实现这种“老派方式”。我想出了以下代码(这次有效!):

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()
4

2 回答 2

5

Scrapy 在后台使用lxml / libxml2,最终调用该node.xpath()方法来执行选择。xpath 表达式中任何带有命名空间的元素都必须加上前缀,并且您必须传递一个映射来告诉选择器每个前缀解析到哪个命名空间。

node.xpath()下面是一个示例来说明在使用该方法时如何将前缀映射到命名空间:

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>]

如果没有使用这个scrapy XMLFeedSpider 类,我猜你的命名空间映射和itertag 需要遵循相同的方案:

class SitemapSpider(XMLFeedSpider):
    namespaces = [
        ('sm', 'http://www.sitemaps.org/schemas/sitemap/0.9'),
        ]
     itertag = 'sm:url'
于 2011-04-08T21:52:32.363 回答
1

我发现 hxs 和 xxs 之间的区别很有帮助。我发现很难找到 xxs 对象。我试图用这个

x = XmlXPathSelector(response)

当这些对我需要的东西来说效果更好时。

hxs.select('//p/text()').extract()

或者

xxs.select('//title/text()').extract()
于 2012-06-21T03:47:09.107 回答