Google App Engine使用Python动态生成Sitemap

时间:2011-03-29 14:21:12

标签: python google-app-engine sitemap

我有一个网站,我想为它创建一个动态的站点地图。我使用带有Python和Django的Google App Engine。

我使用PHP开发了另一个网站,并且由于我在Apache服务器的.htaccess上编写的这个重写规则,我可以访问sitemap.xml。

RewriteRule (.*)\.xml(.*) $1.php$2 [nocase]

.xml文件的生成如下:

$sql_select ="SELECT titulo, title, data_insercao FROM livros l ORDER BY titulo ASC";
$result = mysql_query($sql_select) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$titulo = $row['titulo'];
$title = $row['title'];
$data = $row['data_insercao'];
$sql_comentario ="SELECT data FROM comentarios WHERE livros_title = '" . $title . "' order by data desc LIMIT 0 , 1";
$result_comentario = mysql_query($sql_comentario) or die(mysql_error());
$row_comentario = mysql_fetch_array($result_comentario);
if($row_comentario){
$data = $row_comentario['data'];
}
$pieces = explode(" ", $data);
$data = $pieces[0];
$url_product = 'http://www.sinopsedolivro.net/livro/' . $title . '.html';
echo
' <url>
<loc>'.$url_product.'</loc>
<lastmod>'.$data.'</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
';
}
我的情况(GAE + Python)是否有任何选项,所以当访问www.mydomain.com/sitemap.xml时,他将从服务器接收带有动态内容的.xml文件,使用python生成自己?

3 个答案:

答案 0 :(得分:6)

这是Nick Johnson在站点地图上的精彩文章 Sitemaps

本文介绍了使用post deploy方法生成站点地图,另一种方法是偶尔生成站点地图;每天用cron说一次。

`- description: daily sitemap refresh
  url: /cron_generate_sitemap
  schedule: every day 02:00
  timezone: Pacific/Auckland`

如果您将站点地图存储在数据存储中,那么访问将非常快,这会影响您的搜索引擎优化排名。

我建议您将站点地图存储在一个模型中,例如Nick Johnson提供的模型,例如

`class StaticContent(db.Model):
    """Container for statically served content.  
    The serving path for content is provided in the key name.
    """
    body = db.BlobProperty()
    content_type = db.StringProperty(required=True)
    last_modified = db.DateTimeProperty(required=True, auto_now=True)
    indexed = db.BooleanProperty(required=True, default=True)
`

答案 1 :(得分:3)

当然,您可以使用您的应用程序向任何网址提供您想要的任何回复。只需将控制器映射到/sitemap.xml并编写输出xml的代码。不要忘记将响应的mime类型设置为正确的值。

如果您使用的是django,则可能需要阅读http://docs.djangoproject.com/en/dev/topics/http/urls/

答案 2 :(得分:1)

我遇到了同样的问题,并在python中构建了一个库来动态生成站点地图,此后一直在使用它。


pip install sitemap

用法:

from sitemap import Url, Urlset

urlset = Urlset()
url = Url('https://www.example.com/', changefreq='weekly')

urlset.add_url(url)

# urlset.to_string()
urlset.write_xml('sitemap.xml')

有关更多信息,请访问github上的项目链接:https://github.com/cxmcc/sitemap-python