2017-03-16 87 views
0

嘿,夥計們,所以我嘗試了Python和我有下面的代碼返回我urllib.error.URLERROR:Python的網址有問題

import sys 
import urllib.request 
urlToRead = "http://www.google.ca" 
crawledWebLinks = {} 
while urlToRead !='': 
    try: 
     urlToRead = input("Please enter the next URL to crawl") 
     if urlToRead == "": 
      break 
     shortName = input("Please enter a short name for that Url "+ urlToRead) 
     webFile = urllib.request.urlopen(urlToRead).read() 
     crawledWebLinks[shortName] = webFile 

    except: 
     print("Unexpected Error", sys.exc_info()[0]) 
     stopOrProceed = input("Hmm...stop or proceed? Enter 1 to stop and enter anything else to continue.") 
     if stopOrProceed == 1: 
      print ("Okey-Dokey\n") 
      break 
     else: 
      continue 

print(crawledWebLinks.keys()) 

回答

0

你可以嘗試打印異常消息:

except Exception as e: 
    print("Unexpected Error", e) 

這會給你更多的信息,而不僅僅是異常類。而不僅僅是「URLError」,您可能會看到更多信息,如<urlopen error [Errno -2] Name or service not known>