【发布时间】:2012-08-16 08:09:38
【问题描述】:
可能重复:
web2py url validator
你能帮我处理这段代码吗?
from urllib2 import Request, urlopen, URLError
url = raw_input('enter something')
req = Request(url)
try:
response = urlopen(req)
except URLError, e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
else:
print 'URL is good!'
【问题讨论】:
-
请编辑您的帖子并对其进行格式化以提高可读性 - 您尝试过什么?你的问题到底是什么?
-
我想要求用户输入一个 url 然后验证它..
-
Traceback (most recent call last): File "C:/Users/r00t-7/PycharmProjects/untitled/sample.py", line 7, in <module> response = urlopen(req) File "C:\Python25\lib\urllib2.py", line 121, in urlopen return _opener.open(url, data) File "C:\Python25\lib\urllib2.py", line 366, in open protocol = req.get_type() File "C:\Python25\lib\urllib2.py", line 241, in get_type raise ValueError, "unknown url type: %s" % self.__original ValueError: unknown url type: www.google.com Process finished with exit code 1 -
我认为您的
else子句需要取消缩进。现在它只会打印“Url is good!”当出现 URLError 时。