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.
DjangoBlog/djangoblog/spider_notify.py

26 lines
1.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#gq:
import logging
import requests # HTTP请求库用于向搜索引擎提交URL
from django.conf import settings # 导入Django项目配置
logger = logging.getLogger(__name__) # 初始化日志对象
class SpiderNotify():
"""搜索引擎爬虫通知类向百度等搜索引擎提交新URL加速收录"""
@staticmethod
def baidu_notify(urls):
"""向百度搜索引擎提交URL列表请求收录"""
try:
# 格式化URL数据每行一个URL百度要求的提交格式
data = '\n'.join(urls)
# 发送POST请求到百度收录接口配置在settings.BAIDU_NOTIFY_URL
result = requests.post(settings.BAIDU_NOTIFY_URL, data=data)
logger.info(result.text) # 记录提交结果
except Exception as e:
logger.error(e) # 记录提交失败异常
@staticmethod
def notify(url):
"""通用通知方法:调用百度收录提交(可扩展支持其他搜索引擎)"""
SpiderNotify.baidu_notify(url)