python本地批量驗證url能否能夠正常訪問的方式


 

建立一個名為url.txt的文件,將需要批量測試的url放此文件中,執行腳本就看可以了

 

 

#!/usr/bin/python3
# coding=utf-8

import urllib.request
import urllib.error

f = open("url.txt")
url = []

for line in f.readlines():
    tmp = line.replace('\n','')
    url.append(tmp)
f.close()
for tmpurl in url:
    try:
        req = urllib.request.Request(tmpurl)
        req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36')
        resp = urllib.request.urlopen(req,timeout=2)
        code = resp.getcode()
        print(tmpurl,":",code)
    except urllib.error.URLError as e:
        print(tmpurl,":",e.reason)
    except urllib.error.HTTPError as e:
        print(e.code,":",e.reason)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM