i wrote an easy url validator and shortener in python, how can i save an image in small size from the website url? and recognise that it is not a “file” like .rar or .zip? i apreciate if you edit my code to have a better performance..
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
1) If you are looking to grab and image and resize it, Consider using either PIL or PyImageMagik [Python bindings for the excellent ImageMagik]
2) If you are looking to grab the screenshot of the page, then many people have the asked the same before. You can always use the solutions mentioned in point 1) above after grabbing the screenshot to resize. Webkit2png is good tool to realize the same.
http://www.paulhammond.org/webkit2png/