Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Image
- import sys
- import os
- from BeautifulSoup import BeautifulSoup as bs
- import urlparse
- from urllib2 import *
- from urllib import urlretrieve
- class Ysi(object):
- def __init__(self):
- self.soup = ""
- self.out_folder = "/tmp"
- """ Get picture from the Internet """
- def get_picture(self, url, username="", password=""):
- try:
- self.soup = bs(urlopen(url))
- except IOError, e:
- if not hasattr(e, 'code') or e.code != 401:
- print "This page isn't protected by authentication."
- print 'But we failed for another reason.'
- sys.exit(1)
- # If error was caused by authentication, try to authenticate
- self.__handle_page_authentication(url, username, password)
- parsed = list(urlparse.urlparse(url))
- for image in self.soup.findAll("img"):
- print "Image: %(src)s" % image
- filename = image["src"].split("/")[-1]
- parsed[2] = image["src"]
- outpath = os.path.join(self.out_folder, filename)
- if image["src"].lower().startswith("http"):
- urlretrieve(image["src"], outpath)
- else:
- urlretrieve(urlparse.urlunparse(parsed), outpath)
- ''' this causing a error'''
- self.im = Image.open(image)
- """ Handle possible authentication """
- def __handle_page_authentication(self, url, username, password):
- passman = HTTPPasswordMgrWithDefaultRealm()
- passman.add_password(None, url, username, password)
- authhandler = HTTPBasicAuthHandler(passman)
- opener = build_opener(authhandler)
- install_opener(opener)
- self.soup = bs(urlopen(url))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement