3

我需要从 sitemap.xml 文件中获取帖子。sitemap.xml 文件指向其他站点地图文件。我的蜘蛛如下,它适用于主站点地图文件指向的站点地图之一。

class MySpider(SitemapSpider):
    name = "example"
    allowed_domains = ['www.example.com']

    sitemap_urls = ["http://sitemaps.example.com/post-sitemap1.xml"]
    sitemap_rules = [('\d{4}/\d{2}/\d{2}/\w+', 'parse_post')]

    def parse_post(self, response):
        item = PostItem()
        item['url'] = response.url
        return item

如何让蜘蛛跟随主站点地图文件指向的站点地图文件?主要站点地图文件如下:

<sitemapindex>
    <sitemap>
        <loc>http://sitemaps.example.com/sitemap_recent.xml</loc>               
        <lastmod>2014-09-14T02:15:32-04:00</lastmod></sitemap>
    <sitemap>
        <loc>http://sitemaps.example.com/post-sitemap1.xml</loc>
         <lastmod>2014-09-14T02:15:32-04:00</lastmod></sitemap>  
    </sitemap>
    <sitemap>
          <loc>http://sitemaps.example.com/post-sitemap2.xml</loc>

          <lastmod>2014-02-10T22:50:43-05:00</lastmod>
    </sitemap> 
</sitemapindex>
4

0 回答 0