1
chinesejar Sep 16, 2017
看似没有错误
|
2
qsnow6 Sep 16, 2017
没缩进
|
4
GlobalNPC Sep 16, 2017
```
#!/usr/bin/python ``` 哪里用 python3 了? |
5
everfight Sep 16, 2017
试了下应该是 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> |
7
sunwei0325 Sep 16, 2017
#!/usr/bin/env python3
|
8
Valyrian Sep 16, 2017
支持楼上,用 env
|
9
huoru Sep 16, 2017
f
|
10
huoru Sep 16, 2017
```
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 urllib.error.HTTPError as e: print('error') ``` 是没问题的。 输出的 html 也正常 |