2010-06-24 46 views
3

我对站点地图xml生成和Django的站点地图框架尤其有些怀疑。Django站点地图框架中的静态页面

比方说,我有,博客应用程序,它与每个帖子的内容post_detail网页和一堆像“查看方式标签”帮手'的网页,“由作者观点”等

  1. 是它必须在sitemap.xml中包含每个页面,包括'helper'页面?由于有很多关键字和文本,我希望所有'帮助'页面都被编入索引。我知道sitemaps旨在帮助索引页面,为web爬虫提供一些指导,但不限制爬网。最佳做法是什么?包含所有内容或仅包含重要页面?
  2. 如果可以在sitemap.xml中拥有所有页面,那么将纯文本提交到sitemaps框架中的最佳方式是什么?一种可能的方法是有一个sitemap类,它通过url名称返回反转的url。但它似乎没有DRY,因为我需要第二次注册这些url名称(在url()函数和Sitemap类中)。

我可能可以有一个自定义django.conf.urls.defaults.url函数来注册站点地图的URL映射......您怎么看?

谢谢。

回答

3

网站地图的使用方式由搜索引擎决定。有些只会对网站地图中的内容进行索引,而另一些则会将其用作起点,并基于交叉链接抓取整个网站。

至于包含非生成页面,我们只是创建了django.contrib.sitemaps.Sitemap的子类,并让它读取每行包含一个URL的纯文本文件。喜欢的东西:

class StaticSitemap(Sitemap): 
    priority = 0.8 
    lastmod = datetime.datetime.now() 

    def __init__(self, filename): 
     self._urls = [] 
     try: 
      f = open(filename, 'rb') 
     except: 
      return 

     tmp = [] 
     for x in f: 
      x = re.sub(r"\s*#.*$", '', x) # strip comments 
      if re.match('^\s*$', x): 
       continue # ignore blank lines 
      x = string.strip(x) # clean leading/trailing whitespace 
      x = re.sub(' ', '%20', x) # convert spaces 
      if not x.startswith('/'): 
       x = '/' + x 
      tmp.append(x) 
     f.close() 
     self._urls = tmp 
    # __init__ 

    def items(self): 
     return self._urls 

    def location(self, obj): 
     return obj 

你可以像这样调用它在主站点地图程序:

sitemap['static'] = StaticSitemap(settings.DIR_ROOT +'/sitemap.txt') 

而且我们sitemap.txt文件看起来是这样的:

# One URL per line. 
# All paths start from root - i.e., with a leading/
# Blank lines are OK. 

/tour/ 
/podcast_archive/ 
/related_sites/ 
/survey/ 
/youtube_videos/ 

/teachers/ 
/workshops/ 
/workshop_listing_info/ 

/aboutus/ 
/history/ 
/investment/ 
/business/ 
/contact/ 
/privacy_policy/ 
/graphic_specs/ 
/help_desk/ 
+0

我我真的很抱歉花了很长时间回复,完全忘了它。我不太喜欢这个解决方案,但它是可以接受的。我自己使用了urlresolver,但它也很混乱。所以我仍然怀疑。 – vshulyak 2010-09-05 19:18:50

+0

不用担心。我不是' – 2010-09-05 23:04:14

+0

我也没有爱上它,但是当我们做到了这一点时(2007年夏),它似乎是一种快速实现它的方法。 – 2010-09-05 23:05:15