[英]How can I check if a javascript file exist without downloading it in bash or python
我有一個 javascript URL 列表,我想檢查它們是否正在遠程服務器上退出並且沒有給我 404 未找到。 有時還提供 200k 狀態代碼,但顯示 404 not found 頁面,因為該文件不存在。
wget $url
對我來說很好,但我想下載文件我只想檢查文件是否可以下載。 我嘗試wget --spider $url
但這不適用於許多 URL,例如wget --spider https://www.codecademy.com/cdn-cgi/challenge-platform/h/g/scripts/alpha/invisible.js
output 給我404 not found. broken link!!!
404 not found. broken link!!!
即使當我在瀏覽器中訪問文件時,我也可以看到 javascript 內容!
如何檢查是否可以下載 javascript 文件,而無需將其下載到我的機器上?
您可以使用 python urllib 模塊來檢查給定 URL 中的文件是否存在。 這是一個示例代碼。 結果保存在“結果”字典中。
from urllib.request import urlopen
urls = ['https://www.codecademy.com/cdn-cgi/challenge- platform/h/g/scripts/alpha/invisible.js','https://www.codecademy.com/idontexist.js']
results={}
for url in URLs:
try:
ret = urlopen(URL)
if ret.code == 200:
state="Exists"
print(url+' : {}'.format(state))
except:
state="Not exists"
print(url+' : {}'.format(state))
results[url]=state
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.