当前位置: 首页>>代码示例>>Python>>正文


Python etree._ElementTree方法代码示例

本文整理汇总了Python中lxml.etree._ElementTree方法的典型用法代码示例。如果您正苦于以下问题:Python etree._ElementTree方法的具体用法?Python etree._ElementTree怎么用?Python etree._ElementTree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在lxml.etree的用法示例。


在下文中一共展示了etree._ElementTree方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: format

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementTree [as 别名]
def format(self, diff, orig_tree, differ=None):
        # Make a new tree, both because we want to add the diff namespace
        # and also because we don't want to modify the original tree.
        result = deepcopy(orig_tree)
        if isinstance(result, etree._ElementTree):
            root = result.getroot()
        else:
            root = result

        etree.register_namespace(DIFF_PREFIX, DIFF_NS)

        for action in diff:
            self.handle_action(action, root)

        self.finalize(root)

        etree.cleanup_namespaces(result, top_nsmap={DIFF_PREFIX: DIFF_NS})
        return self.render(result) 
开发者ID:Shoobx,项目名称:xmldiff,代码行数:20,代码来源:formatting.py

示例2: set_trees

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementTree [as 别名]
def set_trees(self, left, right):
        self.clear()

        # Make sure we were passed two lxml elements:
        if isinstance(left, etree._ElementTree):
            left = left.getroot()
        if isinstance(right, etree._ElementTree):
            right = right.getroot()

        if not (etree.iselement(left) and etree.iselement(right)):
            raise TypeError("The 'left' and 'right' parameters must be "
                            "lxml Elements.")

        # Left gets modified as a part of the diff, deepcopy it first.
        self.left = deepcopy(left)
        self.right = right 
开发者ID:Shoobx,项目名称:xmldiff,代码行数:18,代码来源:diff.py

示例3: load_track_from_xml

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementTree [as 别名]
def load_track_from_xml(self, path):
        xml = etree.parse(path)
        root = xml.getroot()  # type: etree._ElementTree
        for child_of_root in root:
            if child_of_root.tag == "s":
                self.s = self.get_1d_array_from_xml(child_of_root)
            elif child_of_root.tag == "phi":
                self.phi = self.get_1d_array_from_xml(child_of_root)
            elif child_of_root.tag == "control_points_s":
                self.control_points_s = self.get_1d_array_from_xml(child_of_root)
            elif child_of_root.tag == "left":
                self.cones_left = self.get_array_from_xml(child_of_root)
            elif child_of_root.tag == "right":
                self.cones_right = self.get_array_from_xml(child_of_root)
            elif child_of_root.tag == "middle":
                self.middle = self.get_array_from_xml(child_of_root)
            elif child_of_root.tag == "control_points":
                self.control_points = self.get_array_from_xml(child_of_root)
        self.computed_cones = True 
开发者ID:AMZ-Driverless,项目名称:fssim,代码行数:21,代码来源:track.py

示例4: match_xpath

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementTree [as 别名]
def match_xpath(xpath, doc):
    """Return a match of expression `xpath` against document `doc`.

    :type xpath: Either `unicode` or `etree.XPath`
    :type doc: Either `etree._ElementTree` or `etree.XPathDocumentEvaluator`

    :rtype: bool
    """
    is_xpath_compiled = is_compiled_xpath(xpath)
    is_doc_compiled = is_compiled_doc(doc)

    if is_xpath_compiled and is_doc_compiled:
        return doc(xpath.path)
    elif is_xpath_compiled:
        return xpath(doc)
    elif is_doc_compiled:
        return doc(xpath)
    else:
        return doc.xpath(xpath) 
开发者ID:maas,项目名称:maas,代码行数:21,代码来源:xpath.py

示例5: try_match_xpath

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementTree [as 别名]
def try_match_xpath(xpath, doc, logger=logging):
    """See if the XPath expression matches the given XML document.

    Invalid XPath expressions are logged, and are returned as a
    non-match.

    :type xpath: Either `unicode` or `etree.XPath`
    :type doc: Either `etree._ElementTree` or `etree.XPathDocumentEvaluator`

    :rtype: bool
    """
    try:
        # Evaluating an XPath expression against a document with LXML
        # can return a list or a string, and perhaps other types.
        # Casting the return value into a boolean context appears to
        # be the most reliable way of detecting a match.
        return bool(match_xpath(xpath, doc))
    except etree.XPathEvalError as error:
        # Get a plaintext version of `xpath`.
        expr = xpath.path if is_compiled_xpath(xpath) else xpath
        logger.warning("Invalid expression '%s': %s", expr, str(error))
        return False 
开发者ID:maas,项目名称:maas,代码行数:24,代码来源:xpath.py

示例6: __init__

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementTree [as 别名]
def __init__(self, input):
        if isinstance(input, etree._Element):
            self.xml_doc = input
        elif isinstance(input, etree._ElementTree):
            self.xml_doc = input.getroot()
        elif KBinXML.is_binary_xml(input):
            self.from_binary(input)
        else:
            self.from_text(input) 
开发者ID:mon,项目名称:kbinxml,代码行数:11,代码来源:kbinxml.py

示例7: test_perform_query

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementTree [as 别名]
def test_perform_query(self):
        api = RomeoAPI(api_key=None)
        with requests_mock.mock() as http_mocker:
            http_mocker.get('http://www.sherpa.ac.uk/romeo/api29.php?issn=0022-328X',
                content=self.issn_response)
            http_mocker.get('http://www.sherpa.ac.uk/romeo/api29.php?jtitle=Physical%20Review%20E',
                content=self.jtitle_response)

            self.assertIsInstance(api.perform_romeo_query(
                {'issn': '0022-328X'}), etree._ElementTree)
            self.assertIsInstance(api.perform_romeo_query(
                {'jtitle': 'Physical Review E'}), etree._ElementTree) 
开发者ID:dissemin,项目名称:dissemin,代码行数:14,代码来源:test_romeo.py

示例8: _get_etree_for_text

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementTree [as 别名]
def _get_etree_for_text(text: str) -> _ElementTree:
    return etree.ElementTree(fromstring(text)) 
开发者ID:HazyResearch,项目名称:fonduer,代码行数:4,代码来源:structural.py

示例9: test_xml

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementTree [as 别名]
def test_xml(self, http):
        request = Request("GET", "https://httpbin.org/xml")
        context_http_response = ContextHttpResponse(http, request)
        assert isinstance(context_http_response.xml, etree._ElementTree) 
开发者ID:alephdata,项目名称:memorious,代码行数:6,代码来源:test_http.py

示例10: load_html

# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import _ElementTree [as 别名]
def load_html(htmlobject):
    """Load object given as input and validate its type
    (accepted: LXML tree, bytestring and string)
    """
    # use tree directly
    if isinstance(htmlobject, (etree._ElementTree, html.HtmlElement)):
        return htmlobject
    tree = None
    check_flag = False
    # try to detect encoding and convert to string
    if isinstance(htmlobject, bytes):
        # test
        if 'html' not in htmlobject[:50].decode(encoding='ascii', errors='ignore'):
            check_flag = True
        guessed_encoding = detect_encoding(htmlobject)
        if guessed_encoding is not None:
            if guessed_encoding == 'UTF-8':
                tree = html.fromstring(htmlobject, parser=HTML_PARSER)
            else:
                try:
                    htmlobject = htmlobject.decode(guessed_encoding)
                    tree = html.fromstring(htmlobject, parser=HTML_PARSER)
                except UnicodeDecodeError:
                    LOGGER.warning('encoding issue: %s', guessed_encoding)
                    tree = html.fromstring(htmlobject, parser=RECOVERY_PARSER)
        else:
            tree = html.fromstring(htmlobject, parser=RECOVERY_PARSER)
    # use string if applicable
    elif isinstance(htmlobject, str):
        # test
        if 'html' not in htmlobject[:50]:
            check_flag = True
        try:
            tree = html.fromstring(htmlobject, parser=HTML_PARSER)
        except ValueError:
            # try to parse a bytestring
            try:
                tree = html.fromstring(htmlobject.encode('utf8'), parser=HTML_PARSER)
            except Exception as err:
                LOGGER.error('parser bytestring %s', err)
        except Exception as err:
            LOGGER.error('parsing failed: %s', err)
    # default to None
    else:
        LOGGER.error('this type cannot be processed: %s', type(htmlobject))
    # further test
    # test if it's HTML
    if tree is not None and check_flag is True:
        if len(tree) < 2:
            LOGGER.error('Parse tree empty: not valid HTML')
            tree = None
    #if tree is None:
    #    if isinstance(htmlobject, bytes) or isinstance(htmlobject, str):
    #        # more robust parsing
    #        tree = fromsoup(htmlobject)
    return tree 
开发者ID:adbar,项目名称:trafilatura,代码行数:58,代码来源:utils.py


注:本文中的lxml.etree._ElementTree方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。