以某種方式檢測 404

以某種方式檢測 404

我讓我的電腦在家裡運作。有時,當我在工作時,我的電腦無法載入我想要載入的頁面。我必須透過 ssh 連接到它,然後重新啟動/重新啟動瀏覽器。這真的很煩人,我正在嘗試找到一種更簡單的方法來做到這一點。我想過截圖,並嘗試檢查它是否看起來像我之前截圖的 404 頁面。但這可能會佔用我的系統資源一點。

我嘗試使用 捕獲頁面標題xdotool,但失敗了。

有沒有辦法偵測404頁面?我在 Ubuntu 上使用 chromium 瀏覽器。由於我的網路連線/計算機,無法從我的電腦存取這些網頁。網頁始終處於開啟狀態,但有時,由於連線問題,瀏覽器會顯示 404 和那個小恐龍。這是我的 xdotool 程式碼:

#!/usr/local/bin/xdotool
a=`xdotool search "http://a.com is not available - Chromium" | head -1`
if [[ $a ]]; then
    echo "yes"
else
    echo "nope"
fi

答案1

怎麼樣wget

三個範例:一個是不存在的頁面,一個是不允許下載的現有頁面,一個是有效的頁面。

獲取https://askubuntu.com/testfor404

--2014-05-09 22:06:20--  https://askubuntu.com/testfor404
Resolving askubuntu.com (askubuntu.com)... 198.252.206.24
Connecting to askubuntu.com (askubuntu.com)|198.252.206.24|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2014-05-09 22:06:21 ERROR 404: Not Found.

獲取https://askubuntu.com/reputation

--2014-05-09 22:07:11--  https://askubuntu.com/reputation
Resolving askubuntu.com (askubuntu.com)... 198.252.206.24
Connecting to askubuntu.com (askubuntu.com)|198.252.206.24|:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2014-05-09 22:07:11 ERROR 403: Forbidden.

獲取http://askubuntu.com

--2014-05-09 22:07:36--  https://askubuntu.com/
Resolving askubuntu.com (askubuntu.com)... 198.252.206.24
Connecting to askubuntu.com (askubuntu.com)|198.252.206.24|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 69629 (68K) [text/html]
Saving to: ‘index.html’

100%[======================================>] 69.629       257KB/s   in 0,3s   

2014-05-09 22:07:36 (257 KB/s) - ‘index.html’ saved [69629/69629]

如果輸出顯示“ERROR 404:Not Found”,則該指令可以擴充為顯示“true”或“false”。


該選項--delete-after將在下載後刪除index.html。--spiderflag 會檢查頁面標題/狀態而不下載。

相關內容