md5使用异常搜索

import httplib
import re 

md5 = raw_input('Enter MD5: ') 

conn = httplib.HTTPConnection("www.md5.rednoize.com")
conn.request("GET", "?q="+ md5) 
try:
     response = conn.getresponse()
     data = response.read() 
     result = re.findall('<div id="result" >(.+?)</div', data)
     print result
except:
     print "couldnt find the hash"

raw_input()
我知道我可能错误地实现了代码,但是我应该使用哪个异常呢?如果它找不到哈希然后引发异常并打印“找不到哈希”     
已邀请:
由于re.findall不会引发异常,因此可能不是您想要检查结果的方式。相反,你可以写一些像
result = re.findall('<div id="result" >(.+?)</div', data)
if result:
    print result
else:
    print 'Could not find the hash'
    
如果你真的喜欢有例外,你必须定义它:
class MyError(Exception):
   def init(self, value):
       self.value = value
   def str(self):
       return repr(self.value)

try: response = conn.getresponse() data = response.read() result = re.findall('(.+?)</div', data) if not result: raise MyError("Could not find the hash") except MyError: raise

    

要回复问题请先登录注册