本文整理汇总了Python中lxml.etree._ElementUnicodeResult方法的典型用法代码示例。如果您正苦于以下问题:Python etree._ElementUnicodeResult方法的具体用法?Python etree._ElementUnicodeResult怎么用?Python etree._ElementUnicodeResult使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lxml.etree
的用法示例。
在下文中一共展示了etree._ElementUnicodeResult方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extract_value
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementUnicodeResult [as 别名]
def extract_value(self, html, is_source=False):
"""
Use css_select or re_select to extract a field value
:return:
"""
value = ''
if self.css_select:
value = html.cssselect(self.css_select)
elif self.xpath_select:
value = html.xpath(self.xpath_select)
else:
raise ValueError('%s field: css_select or xpath_select is expected' % self.__class__.__name__)
if is_source:
return value
if isinstance(value, list) and len(value) == 1:
if isinstance(value[0], etree._Element):
text = ''
for node in value[0].itertext():
text += node.strip()
value = text
if isinstance(value[0], str) or isinstance(value[0], etree._ElementUnicodeResult):
value = ''.join(value)
if self.default is not None:
value = value if value else self.default
return value
示例2: get_value
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementUnicodeResult [as 别名]
def get_value(self, element):
if self.elements:
value = {}
for child in self.elements:
value[child.name] = child.read_value(element)
return value
elif type(element) == etree._ElementStringResult:
value = str(element)
elif type(element) == etree._ElementUnicodeResult:
value = unicode(element)
else:
value = self.element_tostring(element)
return value
示例3: _parse_element
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementUnicodeResult [as 别名]
def _parse_element(self, element):
# Extract text appropriately based on it's type
if isinstance(element, etree._ElementUnicodeResult):
strings = [node for node in element]
else:
strings = [node for node in element.itertext()]
string = "".join(strings)
return string if string else self.default
示例4: xpath
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementUnicodeResult [as 别名]
def xpath(self, selector: str, *, first: bool = False, _encoding: str = None) -> _XPath:
"""Given an XPath selector, returns a list of
:class:`Element <Element>` objects or a single one.
:param selector: XPath Selector to use.
:param first: Whether or not to return just the first result.
:param _encoding: The encoding format.
If a sub-selector is specified (e.g. ``//a/@href``), a simple
list of results is returned.
See W3School's `XPath Examples
<https://www.w3schools.com/xml/xpath_examples.asp>`_
for more details.
If ``first`` is ``True``, only returns the first
:class:`Element <Element>` found.
"""
selected = self.lxml.xpath(selector)
elements = [
Element(element=selection, default_encoding=_encoding or self.encoding)
if not isinstance(selection, etree._ElementUnicodeResult) else str(selection)
for selection in selected
]
return _get_first_or_list(elements, first)
示例5: is_attribute
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementUnicodeResult [as 别名]
def is_attribute(tree, xpath, namespaces):
""" Test if a given xpath matches and that match is an attribute
An xpath attribute search will only match one item"""
if xpath_matches(tree, xpath, namespaces):
match = tree.xpath(xpath, namespaces=namespaces)
if isinstance(match[0], etree._ElementStringResult):
return True
elif isinstance(match[0], etree._ElementUnicodeResult):
return True
return False
示例6: _get_username_from_saml_etree
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementUnicodeResult [as 别名]
def _get_username_from_saml_etree(self, signed_xml):
xpath_with_namespaces = self._make_xpath_builder()
xpath_fun = xpath_with_namespaces(self.xpath_username_location)
xpath_result = xpath_fun(signed_xml)
if isinstance(xpath_result, etree._ElementUnicodeResult):
return xpath_result
if type(xpath_result) is list and len(xpath_result) > 0:
return xpath_result[0]
self.log.warning('Could not find name from name XPath')
return None
示例7: xpath
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementUnicodeResult [as 别名]
def xpath(self, selector: str, *, clean: bool = False, first: bool = False, _encoding: str = None) -> _XPath:
"""Given an XPath selector, returns a list of
:class:`Element <Element>` objects or a single one.
:param selector: XPath Selector to use.
:param clean: Whether or not to sanitize the found HTML of ``<script>`` and ``<style>`` tags.
:param first: Whether or not to return just the first result.
:param _encoding: The encoding format.
If a sub-selector is specified (e.g. ``//a/@href``), a simple
list of results is returned.
See W3School's `XPath Examples
<https://www.w3schools.com/xml/xpath_examples.asp>`_
for more details.
If ``first`` is ``True``, only returns the first
:class:`Element <Element>` found.
"""
selected = self.lxml.xpath(selector)
elements = [
Element(element=selection, url=self.url, default_encoding=_encoding or self.encoding)
if not isinstance(selection, etree._ElementUnicodeResult) else str(selection)
for selection in selected
]
# Sanitize the found HTML.
if clean:
elements_copy = elements.copy()
elements = []
for element in elements_copy:
element.raw_html = lxml_html_tostring(cleaner.clean_html(element.lxml))
elements.append(element)
return _get_first_or_list(elements, first)