建站之後,爲了文章能夠被搜索引擎收錄,我們需要將站點提交到Google、百度、Bing等搜索網站,通過驗證之後,搜索引擎纔會去我們的網站爬數據。
爲了方便爬蟲爬取我們站點裏的文章,我們可以將站點地圖(sitemap.xml)提交到搜索網站。提交之後,爬蟲在光臨我們的網站時,會根據sitemap.xml的指引,抓取所有的URL。
但是,爬蟲光臨我們站點的週期太長(至少要幾天吧),如果想發佈文章之後儘快被搜索引擎收錄,我們可以主動提交URL到搜索網站。Google只能在網頁上操作,百度和Bing都提供了API。下面介紹如何使用API提交URL。
生成url列表
在提交url之前,需先準備好url列表,形如:
http://www.your-site.com/1.html
http://www.your-site.com/2.html
有兩種方式生成url列表。
從sitemap.xml獲取
多數靜態網站構建工具(Hugo、Jekyll)都會生成sitemap.xml,形如:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>https://whuwangyong.github.io/2022-03-29-github-submodule/</loc>
<lastmod>2022-04-27T23:14:33+08:00</lastmod>
<changefreq>weekly</changefreq>
<priority>1</priority>
</url>
<url>
<loc>https://whuwangyong.github.io/2022-04-27-linux-cpu-benchmark/</loc>
<lastmod>2022-04-27T23:14:33+08:00</lastmod>
<changefreq>weekly</changefreq>
<priority>1</priority>
</url>
</urlset>
用以下Linux命令可以將sitemap.xml中的url提取出來:
grep "<loc>" sitemap.xml | grep -v 'html' | awk -F '[< >]' '{print $3}' > urls.txt
這樣做的弊端是每次提交的url列表是全量的。而百度和Bing都限制了每天提交的數量,百度 3000條/天,Bing 100條/天。隨着文章增多,可能會超限。因此,更好的做法是,每次只提交本次更新的文章的url,通過git log
可以實現。
從git log獲取
# 獲取最近一次的commit_id
> git rev-parse --short HEAD
2bfe64f
# 顯示本次提交修改的文件
> git diff-tree --no-commit-id --name-only -r 2bfe64f
2022-03-29-github-submodule/assets/image-20220404003800-3hsabzt.png
2022-03-29-github-submodule/index.html
2022-03-29-github-submodule/index.md
2022-04-27-linux-cpu-benchmark/index.html
2022-04-27-linux-cpu-benchmark/index.md
index.json
sitemap.xml
# 顯示本次提交修改的文件 (另一種方法)
> git show --pretty="" --name-only 2bfe64f
2022-03-29-github-submodule/assets/image-20220404003800-3hsabzt.png
2022-03-29-github-submodule/index.html
2022-03-29-github-submodule/index.md
2022-04-27-linux-cpu-benchmark/index.html
2022-04-27-linux-cpu-benchmark/index.md
index.json
sitemap.xml
然後從修改文件列表中,刪選出.html
結尾的路徑,與站點根目錄拼起來,得到完整url。後文有提供python代碼來生成url列表。
準備好了url列表,下面開始使用API提交。
百度收錄API提交
登陸百度搜索資源平臺-站點管理,選擇資源提交-普通收錄-API提交:
旁邊的sitemap用於提交sitemap.xml文件,這個很簡單,不多說:
百度給出了4種API提交的方法:
curl推送
將要提交的鏈接按照每行一條的格式寫入一個文本文件中,命名此文件爲urls.txt,然後執行:
curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls?site=https://whuwangyong.github.io&token=xxxxx"
post推送
也很簡單,具體可以參考文末代碼。
POST /urls?site=https://whuwangyong.github.io&token=your-token HTTP/1.1
User-Agent: curl/7.12.1
Host: data.zz.baidu.com
Content-Type: text/plain
data = [
"http://www.example.com/1.html",
"http://www.example.com/2.html"
]
php推送、ruby推送
未嘗試。
Bing Webmaster API 提交
登錄Bing Webmaster Tools,點擊右上角設置-API訪問,獲取api key。
然後通過URL提交進入提交API頁面:
Bing提供了兩種提交格式,json和xml。我使用的是json。接口約定如下:
JSON request sample:
POST /webmaster/api.svc/json/SubmitUrlbatch?apikey=sampleapikeyEDECC1EA4AE341CC8B6 HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: ssl.bing.com
{
"siteUrl":"http://yoursite.com",
"urlList":[
"http://yoursite.com/url1",
"http://yoursite.com/url2",
"http://yoursite.com/url3"
]
}
JSON response sample:
HTTP/1.1 200 OK
Content-Length: 10
Content-Type: application/json; charset=utf-8
{
"d":null
}
代碼
def commit_urls():
print("將最新的url提交到百度和bing")
os.system("git checkout gh-pages")
urls = []
# 生成url列表
ret = subprocess.run(
"git rev-parse --short HEAD", stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if ret.returncode == 0:
commit_id = str(ret.stdout, "utf_8").strip()
ret = subprocess.run(
"git show --pretty=" " --name-only " + commit_id,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if ret.returncode == 0:
changes = str(ret.stdout, "utf-8").split("\n")
for change in changes:
if change.endswith(".html"):
# change[:-10] 是爲了去掉末尾的index.html
urls.append("https://whuwangyong.github.io/{}".format(change[:-10]))
else:
print("subprocess run error:{}".format(ret.stderr))
else:
print("subprocess run error:{}".format(ret.stderr))
print("本次提交的urls:", urls)
# 提交到bing
headers = {
"Content-Type": "application/json; charset=utf-8",
"Host": "ssl.bing.com",
}
data = {"siteUrl": "your-site.com", "urlList": urls}
response = requests.post(
url="https://www.bing.com/webmaster/api.svc/json/SubmitUrlbatch?apikey=your-key",
headers=headers,
data=json.dumps(data)
)
print("bing的響應: ", response.content)
# 提交到百度
headers = {
"User-Agent": "curl/7.12.1",
"Host": "data.zz.baidu.com",
"Content-Type": "text/plain"
}
response = requests.post(
url="http://data.zz.baidu.com/urls?site=your-site.com&token=your-token",
headers=headers,
data="\n".join(urls)
)
print("百度的響應: ", response.content)
運行結果:
> python commit_urls.py
將最新的url提交到百度和bing
Switched to branch 'gh-pages'
Your branch is up to date with 'origin/gh-pages'.
本次提交的urls: ['https://whuwangyong.github.io/2022-03-29-github-submodule/', 'https://whuwangyong.github.io/2022-04-27-linux-cpu-benchmark/']
bing的響應: b'{"d":null}'
百度的響應: b'{"remain":2998,"success":2}'
本文同步發佈於: