Google App Engine 使用 Python 动态生成站点地图

发布于 2024-10-27 21:32:52 字数 1324 浏览 4 评论 0原文

我有一个网站,我想为其创建动态站点地图。我将 Google App Engine 与 Python 和 Django 结合使用。

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

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

.xml 文件是这样生成的:

$sql_select ="从 livros l 中选择标题、标题、data_insercao 按标题 ASC 排序";
$结果 = mysql_query($sql_select) 或 die(mysql_error());
while($row = mysql_fetch_array($result)) {
$titulo = $row['titulo'];
$title = $row['title'];
$data = $row['data_insercao'];
$sql_comentario ="从注释中选择数据 WHERE livros_title = '" 。 $标题。 "' 按数据描述 LIMIT 0 , 1 排序";
$result_comentario = mysql_query($sql_comentario) 或 die(mysql_error());
$row_comentario = mysql_fetch_array($result_comentario);
if($row_commentario){
$data = $row_commentario['data'];
}
$pieces = Explode(" ", $data);
$data = $pieces[0];
$url_product = 'http://www.sinopsedolivro.net/livro/' . $标题。 '.html';
回声
' <网址>
'.$url_product.'
'.$data.'
每周
<优先级>0.8

';
}
对于我的情况,是否有任何选项(GAE + Python),以便当访问 www.mydomain.com/sitemap.xml 时,他将从服务器收到一个 .xml 文件,其中包含使用 python 生成的动态内容?

I have a website and I want to create a dynamic sitemap for it. I use Google App Engine with Python and Django.

I developed another website using PHP and the sitemap.xml was accessable because of this Rewrite Rule I wrote on the .htaccess of the Apache Server.

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

The .xml file was generated like this:


$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>
';
}

Is there any option for my case (GAE + Python) so when one access www.mydomain.com/sitemap.xml, he will receive from the server a .xml file with the dynamicaly content generate myself using python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧情别恋 2024-11-03 21:32:52

这是尼克·约翰逊在站点地图上写的一篇很棒的文章
站点地图

文章描述了使用部署后方法生成站点地图,另一种方法是偶尔生成站点地图;例如,

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

如果您将站点地图存储在数据存储中,那么访问速度会非常快,这会影响您的 SEO 排名。

我建议您将站点地图存储在 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)
`

This is a great article by Nick Johnson on sitemaps
Sitemaps

The article describes using a post deploy method of generating the sitemap, an alternative is to generate the sitemap once in a while; say once a day using cron e.g.

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

If you store the sitemap within the data store then access will be very fast which impacts your SEO rankings.

I suggest you store the sitemap in a model such as that provided by Nick Johnson e.g.

`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)
`
送舟行 2024-11-03 21:32:52

当然,您可以通过应用程序对任何 url 提供任何您想要的响应。只需将控制器映射到 /sitemap.xml 并在其中编写输出 xml 的代码即可。不要忘记将响应的 mime 类型设置为正确的值。

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

Sure, you can provide any response you want to any url with your application. Just map your controller to /sitemap.xml and write the code that outputs your xml in there. Don't forget to set the mime type of the response to the right value.

If you are using django, you might want to read http://docs.djangoproject.com/en/dev/topics/http/urls/

遇到 2024-11-03 21:32:52

我遇到了同样的问题,并用 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-蟒蛇

I faced the same problem and built a library in python to dynamically generate sitemaps and have been using it since.


pip install sitemap

Usage:

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')

More info here on, project link on github: https://github.com/cxmcc/sitemap-python

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文