本文整理匯總了Python中cssutils.parseString方法的典型用法代碼示例。如果您正苦於以下問題:Python cssutils.parseString方法的具體用法?Python cssutils.parseString怎麽用?Python cssutils.parseString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cssutils
的用法示例。
在下文中一共展示了cssutils.parseString方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: getMaskedClasses
# 需要導入模塊: import cssutils [as 別名]
# 或者: from cssutils import parseString [as 別名]
def getMaskedClasses(self, soup):
masked_classes = []
mask_style = soup.find_all("style", text=re.compile(r"\.chp\-release\..*?display:none"))
for style in mask_style:
parsed_style = cssutils.parseString(style.get_text())
for rule in parsed_style:
if rule.type == rule.STYLE_RULE:
disp = rule.style.getProperty('display')
if disp and disp.cssValue.cssText.lower() == "none":
for selector in rule.selectorList:
if len(selector.seq) == 2:
root, key = selector.seq
if root.value == ".chp-release" and root.type == 'class':
masked_classes.append(key.value[1:])
print("Masked classes:")
print(masked_classes)
return masked_classes
示例2: test_style_attribute_is_valid_css
# 需要導入模塊: import cssutils [as 別名]
# 或者: from cssutils import parseString [as 別名]
def test_style_attribute_is_valid_css(self):
self.create_user_annotations(1, self.user_a)
anno = UserAnnotation.objects.all().first()
style = parseString(anno.style)
assert style.cssRules[0].valid
示例3: parse
# 需要導入模塊: import cssutils [as 別名]
# 或者: from cssutils import parseString [as 別名]
def parse(self, content, stylesheets=None):
parser = bs4.BeautifulSoup(content, "lxml")
if stylesheets:
# Iterate through each stylesheet, and each rule within each sheet, and apply the relevant styles as
# inline-styles.
docs = (cssutils.parseString(css_content) for css_content in stylesheets if css_content)
for doc in docs:
for rule in (rule for rule in doc.cssRules if rule.typeString == 'STYLE_RULE'):
rule_styles = dict(rule.style)
for selector in rule.selectorList:
elements = parser.select(selector.selectorText)
for element in elements:
style = cssutils.parseStyle(element.attrs.get("style", ""))
element_style = dict(style)
element_style.update(rule_styles)
for key, value in element_style.items():
style[key] = value
element.attrs["style"] = style.getCssText(" ")
tokens = []
for element in parser.childGenerator():
item = self.build_element(element)
if item is None:
continue
tokens.append(item)
tokens = Group(tokens)
normalize_list_elements(tokens)
tokens.set_parents()
correct_whitespace(tokens)
normalize_table_colspans(tokens)
return tokens
示例4: get_body
# 需要導入模塊: import cssutils [as 別名]
# 或者: from cssutils import parseString [as 別名]
def get_body(self, session):
while not self.new_urls.empty():
print(animation[self.itr % len(animation)], end="\r")
self.itr = self.itr + 1
current_url, level = await self.new_urls.get()
if current_url.human_repr() in self.visited_urls:
continue
self.visited_urls.append(current_url.human_repr())
file_name, hash_name = self._make_filename(current_url)
self.logger.debug('Cloned file: %s', file_name)
data = None
content_type = None
try:
response = await session.get(current_url, headers={'Accept': 'text/html'}, timeout=10.0)
headers = self.get_headers(response)
content_type = response.content_type
data = await response.read()
except (aiohttp.ClientError, asyncio.TimeoutError) as client_error:
self.logger.error(client_error)
else:
await response.release()
if data is not None:
self.meta[file_name]['hash'] = hash_name
self.meta[file_name]['headers'] = headers
self.counter = self.counter + 1
if content_type == 'text/html':
soup = await self.replace_links(data, level)
data = str(soup).encode()
elif content_type == 'text/css':
css = cssutils.parseString(data, validate=self.css_validate)
for carved_url in cssutils.getUrls(css):
if carved_url.startswith('data'):
continue
carved_url = yarl.URL(carved_url)
if not carved_url.is_absolute():
carved_url = self.root.join(carved_url)
if carved_url.human_repr() not in self.visited_urls:
await self.new_urls.put((carved_url, level + 1))
with open(os.path.join(self.target_path, hash_name), 'wb') as index_fh:
index_fh.write(data)
示例5: generateFontCSS
# 需要導入模塊: import cssutils [as 別名]
# 或者: from cssutils import parseString [as 別名]
def generateFontCSS(self):
# Список стилей для встраивания шрифтов
style_rules = ['.titleblock', '.text-author', 'p', 'p.title', '.cite', '.poem', '.table th', '.table td', '.annotation', 'body']
css_string = modules.default_css.gui_default_css
css = cssutils.parseString(css_string)
font_regular = ''
font_italic = ''
font_bold = ''
font_bolditalic = ''
if 'Regular' in self.gui_config.fontDb.families[self.gui_config.embedFontFamily]:
font_regular = self.gui_config.fontDb.families[self.gui_config.embedFontFamily]['Regular']
if 'Italic' in self.gui_config.fontDb.families[self.gui_config.embedFontFamily]:
font_italic = self.gui_config.fontDb.families[self.gui_config.embedFontFamily]['Italic']
else:
font_italic = font_regular
if 'Bold' in self.gui_config.fontDb.families[self.gui_config.embedFontFamily]:
font_bold = self.gui_config.fontDb.families[self.gui_config.embedFontFamily]['Bold']
else:
font_bold = font_regular
if 'Bold Italic' in self.gui_config.fontDb.families[self.gui_config.embedFontFamily]:
font_bolditalic = self.gui_config.fontDb.families[self.gui_config.embedFontFamily]['Bold Italic']
else:
font_bolditalic = font_italic
css.add('@font-face {{ font-family: "para"; src: url("fonts/{0}"); }}'.format(font_regular))
css.add('@font-face {{ font-family: "para"; src: url("fonts/{0}"); font-style: italic; }}'.format(font_italic))
css.add('@font-face {{ font-family: "para"; src: url("fonts/{0}"); font-weight: bold; }}'.format(font_bold))
css.add('@font-face {{ font-family: "para"; src: url("fonts/{0}"); font-style: italic; font-weight: bold; }}'.format(font_bolditalic))
found_body = False
for rule in css:
if rule.type == rule.STYLE_RULE:
if rule.selectorText in style_rules:
rule.style['font-family'] = '"para"'
if rule.selectorText == 'body':
found_body = True
# Добавим стиль для
if not found_body:
css.add('body {font-family: "para"; line-height: 100%; }')
css_path = os.path.join(os.path.dirname(self.config_file), 'profiles')
if not os.path.exists(css_path):
os.makedirs(css_path)
with codecs.open(os.path.join(css_path, '_font.css'), 'w', 'utf-8') as f:
f.write(str(css.cssText, 'utf-8'))