本文整理匯總了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)