# coding=utf-8 __author__ = 'Kang' import urllib import mylogto # 2 定义一个函数func(urllist) urllist:为URL的列表,例如:['http://xx.com','http://www.xx.com','http://www.xxx.com'...] # 函数功能:要求依次打开url,打印url对应的内容,如果有的url打不开,则把url记录到日志文件里,并且跳过继续访问下个url。 mlog = mylogto.get_mylog(__file__) # UnboundLocalError: local variable 'content' referenced before assignment def func(url_list): for x in url_list: try: content = urllib.urlopen(x) print content.read() except IOError: mlog.debug("can't opne %s" % x) continue finally: content.close() url_list = ['haidu.com', 'http://www.taobao.com', 'http://www.qq.com'] func(url_list)
#it is test for my module mylogto # coding=utf-8 __author__ = 'Kang' import mylogto pp = __file__ mylog = mylogto.get_mylog(pp) ls = [1, 2, 3, 4] try: print ls[6] except IndexError as e: mylog.debug('the error is %s'%e)