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)
你有什么心态,就有什么样的人生。管不好自己的心态,注定你就是个弱者。