Commits

_vios committed 4a61111

無効なURLを入力したときの例外処理を追加

Comments (0)

Files changed (1)

 WEB_DB_NAME = "web.db"
 
 def getWebHash(url):
-    content = urllib2.urlopen(url)
+    try:
+        content = urllib2.urlopen(url)
+    except urllib2.URLError:
+        return False
     return hashlib.md5(content.read()).hexdigest()
 
 def updateWebDB(url):
     if not os.path.exists(WEB_DB_NAME):
         makeTable()
     hash = getWebHash(url)
+    if hash == False:
+        print u"入力されたURLは恐らく無効なURLです"
+        print u"処理を中断します..."
+        return
+
     con = sqlite3.connect(WEB_DB_NAME,isolation_level=None)
     c = con.cursor()
     c.execute("select * from webpage where url='%s'" % url)
 if __name__ == "__main__":
     updateWebDB("http://google.co.jp")
     dumpDB()
-    isUpdate("http://google.co.jp")