1
chinesejar 2017 年 9 月 16 日
看似没有错误
|
2
qsnow6 2017 年 9 月 16 日
没缩进
|
4
GlobalNPC 2017 年 9 月 16 日
```
#!/usr/bin/python ``` 哪里用 python3 了? |
5
everfight 2017 年 9 月 16 日
试了下应该是 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 2017 年 9 月 16 日
#!/usr/bin/env python3
|
8
Valyrian 2017 年 9 月 16 日
支持楼上,用 env
|
9
ChristopherWu 2017 年 9 月 16 日
f
|
10
ChristopherWu 2017 年 9 月 16 日
```
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 也正常 |