Привет, я новичок в python и купил онлайн-курс, и в нем показано, как создать программу, которая просит пользователя ввести URL-адрес, затем проверяет его действительность, а затем импортирует его в текстовый файл. это работает для инструктора, используя python 2.7, я использую то же самое, но не буду работать для меня, и теперь я не могу с этим справиться, пожалуйста, помогите.
import urllib2
import sys
urlToRead = 'http://www.google.com'
# this value wont actually get used, because of the way the while loop below is setup.
# but while loops often need a dummy value like this to work right the first time.
while urlToRead != " ":
try:
urlToRead = input("Please enter the Next URL to crawl ")
if urlToRead == "":
print ("OK, Exiting loop")
break
shortName = input("Please enter a short name for that URL " + urlToRead)
webFile = urllib2.urlopen(urlToRead).read()
#This line above uses a readymade function in the urllib2 module to do something super - cool:
#Ot takes a url, goes to the website for that url, downloads the contents(which are in HTML format)
# and returns them to be stored in a string variable (here called webFile)
crawledWebLinks[shortName] = webFile
# this line above places a key value pair (shrtname, html for that url) in the dictionary
except:
# this bit of caode - the intended lines following 'except:' will be executed if the code
# in the 'try' block (the intended lines following) the 'try:' above throw and error
# this is an example of something know as exception-handling, on which we will talk later
print ("*********************\nUnexpected Error***********") ,sys.exc_info()[0]
# The snip 'sys.exc_info()[0]' returns information about the last error that occurred-
# this code is made available through the sys library that we imported above.
# Quite Magical
stopOrProceed = input("Hmm..stop or proceed? Enter 1 to stop, enter anything else to continue")
if stopOrProceed ==1 :
print ("Okey-Dokey...Stopping\n")
break
# this break will brake out of the nearest loop - in this case, the while loop
else:
print ("Cool! Lets continue\n")
continue
# this continue will skip out of the current iteration of this loop and move to the
# next i.e. the loop will reset to the start
print ("Note this indentation - this line is inside the while loop, but it is outside the try/catch block")
print ("Note this indentation - this line is entirely outside the loop ")
print (crawledWebLinks.keys())
output
Please enter the Next URL to crawl "http://www.google.com"
Please enter a short name for that URL http://www.google.com "google"
*********************
Unexpected Error*********** <type 'exceptions.NameError'>
Hmm..stop or proceed? Enter 1 to stop, enter anything else to continue