标签:class lib reason html gen etc close python window
建立一个名为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)
标签:class lib reason html gen etc close python window
原文地址:https://www.cnblogs.com/LQZ888/p/12109085.html