本文目录导读:

我来详细介绍如何使用脚本自动提交网站到主流搜索引擎。
提交到百度搜索
使用curl脚本 (Linux/Mac)
#!/bin/bash
# 百度站长平台API提交脚本
# 需要在百度站长平台获取token
DOMAIN="your-domain.com"
TOKEN="your-baidu-token"
API_URL="http://data.zz.baidu.com/urls?site=$DOMAIN&token=$TOKEN"
# 要提交的URL列表
URLS=(
"https://$DOMAIN/"
"https://$DOMAIN/about"
"https://$DOMAIN/contact"
# 添加更多URL
)
# 将URL列表转换为换行分隔的字符串
URL_LIST=$(printf "%s\n" "${URLS[@]}")
# 提交到百度
curl -H "Content-Type: text/plain" \
--data "$URL_LIST" \
"$API_URL"
Python版本
#!/usr/bin/env python3
import requests
def submit_to_baidu(domain, token, urls):
api_url = f"http://data.zz.baidu.com/urls?site={domain}&token={token}"
url_text = "\n".join(urls)
headers = {
"Content-Type": "text/plain"
}
response = requests.post(api_url, data=url_text, headers=headers)
return response.json()
# 使用示例
domain = "your-domain.com"
token = "your-baidu-token"
urls = [
f"https://{domain}/",
f"https://{domain}/about",
]
result = submit_to_baidu(domain, token, urls)
print(result)
提交到Google Search Console
Python脚本(使用Google API)
#!/usr/bin/env python3
import json
from google.oauth2 import service_account
from googleapiclient.discovery import build
def submit_to_google(site_url, service_account_file, urls):
"""使用Google Indexing API提交URL"""
# 加载服务账号凭据
credentials = service_account.Credentials.from_service_account_file(
service_account_file,
scopes=['https://www.googleapis.com/auth/indexing']
)
# 创建API服务
service = build('indexing', 'v3', credentials=credentials)
for url in urls:
body = {
'url': url,
'type': 'URL_UPDATED'
}
try:
response = service.urlNotifications().publish(body=body).execute()
print(f"提交成功: {url}")
except Exception as e:
print(f"提交失败 {url}: {e}")
# 使用示例
site_url = "https://your-domain.com"
service_account_file = "path/to/service-account-key.json"
urls = [
"https://your-domain.com/",
"https://your-domain.com/new-page",
]
submit_to_google(site_url, service_account_file, urls)
提交到必应
Python脚本
#!/usr/bin/env python3
import requests
def submit_to_bing(api_key, site_url, urls):
"""提交到必应Webmaster"""
endpoint = "https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlBatch"
headers = {
"Content-Type": "application/json",
"api-key": api_key
}
data = {
"siteUrl": site_url,
"urlList": urls
}
response = requests.post(endpoint, json=data, headers=headers)
return response.json()
# 使用示例
api_key = "your-bing-api-key"
site_url = "https://your-domain.com"
urls = [
"https://your-domain.com/",
"https://your-domain.com/blog",
]
result = submit_to_bing(api_key, site_url, urls)
print(result)
综合提交脚本(支持多个搜索引擎)
Python完整脚本
#!/usr/bin/env python3
"""
综合URL提交脚本 - 支持百度、Google、必应
"""
import requests
import json
import os
from typing import List
class URLSubmitter:
def __init__(self, config_file='submit_config.json'):
self.load_config(config_file)
def load_config(self, config_file):
"""加载配置文件"""
with open(config_file, 'r') as f:
self.config = json.load(f)
def submit_to_baidu(self, urls: List[str]):
"""提交到百度"""
print("正在提交到百度...")
try:
api_url = f"http://data.zz.baidu.com/urls?site={self.config['baidu']['domain']}&token={self.config['baidu']['token']}"
url_text = "\n".join(urls)
headers = {"Content-Type": "text/plain"}
response = requests.post(api_url, data=url_text, headers=headers)
print(f"百度响应: {response.json()}")
return True
except Exception as e:
print(f"百度提交失败: {e}")
return False
def submit_to_bing(self, urls: List[str]):
"""提交到必应"""
print("正在提交到必应...")
try:
endpoint = "https://ssl.bing.com/webmaster/api.svc/json/SubmitUrlBatch"
headers = {
"Content-Type": "application/json",
"api-key": self.config['bing']['api_key']
}
data = {
"siteUrl": self.config['bing']['site_url'],
"urlList": urls
}
response = requests.post(endpoint, json=data, headers=headers)
print(f"必应响应: {response.status_code}")
return True
except Exception as e:
print(f"必应提交失败: {e}")
return False
def submit_all(self, urls: List[str]):
"""提交到所有搜索引擎"""
print(f"开始提交 {len(urls)} 个URL...")
success = []
if self.config.get('baidu', {}).get('enabled'):
success.append(('百度', self.submit_to_baidu(urls)))
if self.config.get('bing', {}).get('enabled'):
success.append(('必应', self.submit_to_bing(urls)))
print("\n提交结果:")
for engine, result in success:
print(f"{engine}: {'成功' if result else '失败'}")
# 配置文件示例 (submit_config.json)
"""
{
"baidu": {
"enabled": true,
"domain": "your-domain.com",
"token": "your-baidu-token"
},
"bing": {
"enabled": true,
"site_url": "https://your-domain.com",
"api_key": "your-bing-api-key"
}
}
"""
def get_urls_from_sitemap(sitemap_url):
"""从sitemap获取URL列表"""
import xml.etree.ElementTree as ET
try:
response = requests.get(sitemap_url)
root = ET.fromstring(response.content)
# 解析XML,获取所有url标签
urls = []
for url in root.iter('{http://www.sitemaps.org/schemas/sitemap/0.9}loc'):
urls.append(url.text)
return urls
except Exception as e:
print(f"获取sitemap失败: {e}")
return []
def main():
# 创建提交器
submitter = URLSubmitter()
# 方式1: 手动指定URL
urls = [
"https://your-domain.com/",
"https://your-domain.com/blog",
]
# 方式2: 从sitemap获取URL
# urls = get_urls_from_sitemap("https://your-domain.com/sitemap.xml")
# 提交到所有搜索引擎
submitter.submit_all(urls)
if __name__ == "__main__":
main()
自动化部署建议
使用crontab定时执行(Linux)
# 每天凌晨3点执行 0 3 * * * /usr/bin/python3 /path/to/submit_script.py >> /var/log/url_submit.log 2>&1
使用Windows Task Scheduler
# Windows PowerShell脚本 $action = New-ScheduledTaskAction -Execute "python.exe" -Argument "C:\scripts\submit_urls.py" $trigger = New-ScheduledTaskTrigger -Daily -At 3am Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "URLSubmitTask" -Description "自动提交URL到搜索引擎"
注意事项
- API限制:各搜索引擎都有提交频率和数量限制
- URL质量:只提交高质量、已更新的页面
- 错误处理:添加重试机制和日志记录
- 安全配置:不要在脚本中硬编码API密钥,使用环境变量或配置文件
- 合规性:遵守各搜索引擎的使用条款
这些脚本可以作为基础,根据实际需求进行调整和扩展。