0
我写了一个简单的URL验证和缩写在python中,我怎么能从网站的url保存图像的小尺寸?并认识到它不是像.rar或.zip这样的“文件”?我apreciate如果编辑我的代码有更好的表现..在python中的URL验证和缩短
from urllib2 import Request, urlopen, URLError
from urlparse import urlparse
import string
import random
url = raw_input('plZ enter the url: ').lower() #get input and convert to lowercase
while True:
if url[0:7] == 'http://' or url[0:8] == 'https://' or url[0:6] =='ftp://': #check the url protocol
try: #try to open url
response = urlopen(url)
parsed_url = urlparse(url)
rand_url = ''.join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for x in range(6))
print " The shortened url is: http://url.com/" + rand_url
print "\n Original URL is: "+url
exit()
except URLError, e: #except the error by asking the address again
if hasattr(e, 'reason'):
print "URL is not valid or server is NOT responsive..plZ try again.."
url = raw_input('plZ enter the url: ').lower()
#print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request.' #message in case of server or connection error
print 'Error code: ', e.code
else:
print "\n protocol missing, using HTTP instead.. \n"
url = "http://"+url
你想想图标或者说整个页面的图像,因为它是由歌剧院,Firefox或Chrome表明,他们显示了“快速拨号”? – 2012-08-16 12:19:42
恰好像歌剧和火狐的形象。 – 2012-08-16 12:25:44
不要使用'lower()'。网址是区分大小写的,原则上通常在实践中。 – alexis 2012-08-16 20:49:05