跳转至

Django搜索优化sitemap、RSS、robots

站点地图 sitemap.xml

用途

告诉搜索引擎站点的目录结构,方便搜索引擎收录站点。

实现

urls.py:

from django.contrib.sitemaps.views import sitemap
from common.sitemaps import sitemaps
urlpatterns = [
    path('', IndexView.as_view(), name='index'),
    path('admin/', admin.site.urls),
    ...
    path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='sitemap'),
]

common/sitemaps.py

from django.contrib.sitemaps import Sitemap
from django.urls import reverse_lazy

from article.models import Article


class StaticViewSitemap(Sitemap):
    priority = 0.5
    changefreq = 'weekly'

    def items(self):
        # 静态url的name
        return ['index', 'article_index', 'article_all', 'news_index', 'user_login', 'user_register',
                'user_forget_pwd', 'version', 'rss']

    def location(self, item):
        return reverse_lazy(item)


class ArticleSitemap(Sitemap):
    priority = 0.5
    changefreq = 'weekly'

    def items(self):
        return Article.objects.filter(is_public=True).all().order_by('-id')

    def location(self, item):
        return reverse_lazy('article_detail', kwargs={'pk': item.id})


sitemaps = {
    'static': StaticViewSitemap,
    'article': ArticleSitemap,
    ...
}

RSS订阅

用途

让订阅你站点的用户能第一时间收到站点信息更新提醒,相当于站点目录的功能。

实现

from common.feeds import BlogFeed
urlpatterns = [
    path('', IndexView.as_view(), name='index'),
    path('admin/', admin.site.urls),
    ...
    path('rss/', BlogFeed(), name='rss'),
]

common/feeds.py:

from django.conf import settings
from django.urls import reverse_lazy
from django.utils.feedgenerator import Rss201rev2Feed
from django.contrib.syndication.views import Feed

from article.models import Article


class BlogFeed(Feed):
    feed_type = Rss201rev2Feed
    link = reverse_lazy('index')
    feed_url = reverse_lazy('rss')

    def title(self):
        return get_value(settings.WEBSITE_TITLE)

    def description(self):
        return get_value(settings.WEBSITE_SEO_DESCRIPTION)

    def items(self):
        return Article.objects.order_by('-pk')[:10]

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        return item.summary

    def item_link(self, item):
        return reverse_lazy('article_detail', kwargs={'pk': item.id})

    def feed_copyright(self):
        return "Copyright © 2015-2018 yinkh"

robots.txt

用途

robots.txt配合站点地图sitemaps.xml使用,用于告诉搜索引擎,哪些路径下的资源可以抓取,哪些路径下的资源不可以抓取。

实现方法

方法1Django提供robots.txt文件(不推荐):

urls.py:

from django.views.generic import TemplateView

path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type="text/plain"), name="robots"),

方法2 robots.txt文件是静态文件,直接由web服务器分发会更有效(推荐)。

Nginx:

location  /robots.txt {
    alias  /path/to/static/robots.txt;
}

Apache2:

Alias /robots.txt /django/Blog/static/robots.txt

robots.txt文本内容

robots.txt

User-agent: *
Disallow:

用法简介:

User-agent是用来匹配爬虫的,每个爬虫都会有一个名字,如果你有安装awstats统计工具,你就能查看到爬虫的名字,比如百度的爬虫叫BaiDuSpider,Google的爬虫叫Googlebot,*表示所有爬虫。

Disallow表示禁止爬虫访问的目录。Disallow: /表示拦截整站。

Allow表示允许爬虫访问的目录。Allow: /表示允许整站。

Sitemap用来指定sitemap的位置。

Crawl-delay用来告诉爬虫两次访问的间隔,单位是秒。爬虫如果爬得很勤,对动态网站来说,压力有点大,可能会导致服务器负载增高,用户访问变慢。

django serving robots.txt efficiently

robots.txt用法