试了下应该是 error 的类型错了
#!/usr/bin/python
import urllib.request
import urllib.error
try:
response = urllib.request.urlopen('
https://www.baidu.com',timeout=2)
html = response.read().decode('utf-8')
print(html)
except:
print('error')
打印的结果如下:
<html>
<head>
<script>
location.replace(location.href.replace("https://","http://"));
</script>
</head>
<body>
<noscript><meta http-equiv="refresh" content="0;url=
http://www.baidu.com/"></noscript>
</body>
</html>