def main(args=None): usage = "usage: %prog [options] URL" parser = optparse.OptionParser(usage=usage) parser.add_option('-d', '--debug', action='store_true', dest='debug', help='show debug messages during capturing') parser.add_option('-m', '--minified', action='store_true', dest='minified', help='saves minified version of captured files') parser.add_option('-n', '--notsave', action='store_true', dest='notsave', help='if given files are NOT saved, only log is written') # parser.add_option('-r', '--saveraw', action='store_true', dest='saveraw', # help='if given saves raw css otherwise cssutils\' parsed files') parser.add_option('-s', '--saveto', action='store', dest='saveto', help='saving retrieved files to "saveto", defaults to "_CSSCapture_SAVED"') parser.add_option('-u', '--useragent', action='store', dest='ua', help='useragent to use for request of URL, default is urllib2s default') options, url = parser.parse_args() # TODO: options.saveraw = False if not url: parser.error('no URL given') else: url = url[0] if options.debug: level = logging.DEBUG else: level = logging.INFO # START c = CSSCapture(ua=options.ua, defaultloglevel=level) stylesheetlist = c.capture(url) if options.notsave is None or not options.notsave: if options.saveto: saveto = options.saveto else: saveto = u'_CSSCapture_SAVED' c.saveto(saveto, saveraw=options.saveraw, minified=options.minified) else: for i, s in enumerate(stylesheetlist): print u'''%s. encoding: %r title: %r href: %r''' % (i + 1, s.encoding, s.title, s.href)
def generateCSS(self): c = CSSCapture(ua='Mozilla/5.0 (X11; U; Linux i686; es-ES; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3') sheetList = c.capture(self.url) page = FetchURL(self.url).data soup = BeautifulSoup(page) styleElementList = soup.findAll('style') for styleElement in styleElementList: css = styleElement.renderContents() sheetList.append(cssutils.parseString(css)) for sheet in sheetList: if not('handheld' in sheet.media.mediaText or 'print' in sheet.media.mediaText) or 'screen' in sheet.media.mediaText: for rule in sheet: if rule.type == 1: for property in rule.style: if property.name == 'color' or property.name == 'background-color': self.generatedCSS = self.generatedCSS + rule.selectorText + '{'+ property.name +':' + property.value +';}\n' return self.generatedCSS