错误处理
from urllib import request,error
import requests
from requests import exceptions
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'
}
url = 'http://www.mobiletrain.org/?pinzhuanbdtg=biaoti'
url2 = 'http://www.mobiletrain.org/pppp?pinzhuanbdtg=biaoti'
url3 = 'http://www.mobiletrain .org/?pinzhuanbdtg=biaoti'
try:
req = request.Request(url=url,headers=headers)
request.urlopen(req)
except error.HTTPError as e:
print('请求出错!')
print(e)
except error.URLError as f:
print('url出错')
print(f)
try:
requests.get(url=url,headers=headers,timeout=0.00001)
except exceptions.Timeout as g:
print(g)
except Exception as h:
print(h)