import urllib.request
import time
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/49.0.2')]
#這個是你放網址的文件名,改過來就能夠了
# file = open('test.txt')
# lines = file.readlines()
aa=['http://www.jd.com/','https://top.jd.com/?cateId=794&itemId=6254701']
# for line in lines:
# temp=line.replace('\n','')
# aa.append(temp)
# print(aa)
print('開始檢查:')
for a in aa:
tempUrl = a
try :
opener.open(tempUrl)
print(tempUrl+'沒問題')
except urllib.error.HTTPError:
print(tempUrl+'=訪問頁面出錯')
time.sleep(2)
except urllib.error.URLError:
print(tempUrl+'=訪問頁面出錯')
time.sleep(2)
time.sleep(0.1) app