You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Django/doc/djangoblog/spider_notify.py

40 lines
1.0 KiB

import logging
import requests
from django.conf import settings
# 获取日志记录器
logger = logging.getLogger(__name__)
class SpiderNotify():
"""搜索引擎爬虫通知类"""
@staticmethod
def baidu_notify(urls):
"""
向百度站长平台提交链接,通知百度爬虫抓取更新内容
Args:
urls: 需要通知的URL列表
"""
try:
# 将URL列表转换为换行分隔的字符串格式
data = '\n'.join(urls)
# 向百度站长平台API提交URL数据
result = requests.post(settings.BAIDU_NOTIFY_URL, data=data)
# 记录API返回结果
logger.info(result.text)
except Exception as e:
# 记录通知过程中的错误
logger.error(e)
@staticmethod
def notify(url):
"""
通用的爬虫通知方法(目前仅支持百度)
Args:
url: 需要通知的URL
"""
SpiderNotify.baidu_notify(url)