1. 注册账号:https://search.google.com/search-console
2. 提交 sitemap
1. 注册账号:https://ziyuan.baidu.com/site/index
2. 主动推送
3. 百度推送相关代码
# spider_notify.py (推送类)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from django.contrib.sitemaps import ping_google
from django.conf import settings
import requests
from logger.logger import logger
class SpiderNotify:
@staticmethod
def baidu_notify(urls):
try:
data = '\n'.join(urls)
result = requests.post(settings.BAIDU_NOTIFY_URL, data=data)
logger.info(result.text)
except Exception as e:
logger.error(e)
@staticmethod
def __google_notify():
try:
ping_google('/sitemap.xml')
except Exception as e:
logger.error(e)
@staticmethod
def notify(url):
SpiderNotify.baidu_notify(url)
SpiderNotify.__google_notify()
# blog_signals.py( model 保存时 触发信号)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: yinzhuoqun
@site: http://zhuoqun.info/
@email: yin@zhuoqun.info
@time: 2019/6/11 17:15
"""
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.conf import settings
from joyoo.spider_notify import SpiderNotify
from logger.logger import logger
from blog.models import Article
# dispatch_uid 确保此 receiver 只调用一次
@receiver(post_save, sender=Article, dispatch_uid="model_post_save_callback")
def model_post_save_callback(sender, instance, created, **kwargs):
if 'get_full_url' in dir(instance):
# if settings.DEBUG:
try:
notify_url = instance.get_full_url()
# logger.info(notify_url)
SpiderNotify.baidu_notify([notify_url])
except Exception as e:
logger.error("notify baidu sipder:", e)