本文整理汇总了Python中lxml.etree.XPath方法的典型用法代码示例。如果您正苦于以下问题:Python etree.XPath方法的具体用法?Python etree.XPath怎么用?Python etree.XPath使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lxml.etree
的用法示例。
在下文中一共展示了etree.XPath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def post(self, html):
"""
Try to play with request ...
"""
import urllib2
response = urllib2.urlopen('file://%s' % html)
data = response.read()
post = etree.HTML(data)
# find text function
find_text = etree.XPath("//text()", smart_strings=False)
LOG.info(find_text(post))
post.clear()
示例2: test_parse_rule
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def test_parse_rule():
"""Ensure parse_rule returns expected output."""
expr = XPath("//Num")
assert parse_rule(
rule_name='',
rule_values=dict(
description='',
expr=expr,
example="a = 1",
instead="a = int('1')",
settings=Settings(included=[], excluded=[], allow_ignore=True),
)
) == Rule(
name='',
description='',
expr=expr,
example="a = 1",
instead="a = int('1')",
settings=Settings(included=[], excluded=[], allow_ignore=True)
)
示例3: _details_prepare_merge
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def _details_prepare_merge(details):
# We may mutate the details later, so copy now to prevent
# affecting the caller's data.
details = details.copy()
# Prepare an nsmap in an OrderedDict. This ensures that lxml
# serializes namespace declarations in a stable order.
nsmap = OrderedDict((ns, ns) for ns in sorted(details))
# Root everything in a namespace-less element. Setting the nsmap
# here ensures that prefixes are preserved when dumping later.
# This element will be replaced by the root of the lshw detail.
# However, if there is no lshw detail, this root element shares
# its tag with the tag of an lshw XML tree, so that XPath
# expressions written with the lshw tree in mind will still work
# without it, e.g. "/list//{lldp}something".
root = etree.Element("list", nsmap=nsmap)
# We have copied details, and root is new.
return details, root
示例4: _details_do_merge
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def _details_do_merge(details, root):
# Merge the remaining details into the composite document.
for namespace in sorted(details):
xmldata = details[namespace]
if xmldata is not None:
try:
detail = etree.fromstring(xmldata)
except etree.XMLSyntaxError as e:
maaslog.warning("Invalid %s details: %s", namespace, e)
else:
# Add the namespace to all unqualified elements.
for elem in detail.iter("{}*"):
elem.tag = etree.QName(namespace, elem.tag)
root.append(detail)
# Re-home `root` in a new tree. This ensures that XPath
# expressions like "/some-tag" work correctly. Without this, when
# there's well-formed lshw data -- see the backward-compatibilty
# hack futher up -- expressions would be evaluated from the first
# root created in this function, even though that root is now the
# parent of the current `root`.
return etree.ElementTree(root)
示例5: merge_details_cleanly
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def merge_details_cleanly(details):
"""Merge node details into a single XML document.
`details` should be of the form::
{"name": xml-as-bytes, "name2": xml-as-bytes, ...}
where `name` is the namespace (and prefix) where each detail's XML
should be placed in the composite document; elements in each
detail document without a namespace are moved into that namespace.
This is similar to `merge_details`, but the ``lshw`` detail is not
treated specially. The result of this function is not compatible
with XPath expressions created for old releases of MAAS.
The returned document is always rooted with a ``list`` element.
"""
details, root = _details_prepare_merge(details)
return _details_do_merge(details, root)
示例6: match_xpath
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [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)
示例7: try_match_xpath
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [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
示例8: scenario
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def scenario(name, xpath, doc, expected_result, expected_log=""):
"""Return a scenario (for `testscenarios`) to test `try_match_xpath`.
This is a convenience function to reduce the amount of
boilerplate when constructing `scenarios_inputs` later on.
The scenario it constructs defines an XML document, and XPath
expression, the expectation as to whether it will match or
not, and the expected log output.
"""
doc = etree.fromstring(doc).getroottree()
return (
name,
dict(
xpath=xpath,
doc=doc,
expected_result=expected_result,
expected_log=dedent(expected_log),
),
)
# Exercise try_match_xpath with a variety of different inputs.
示例9: populate_tag_for_multiple_nodes
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def populate_tag_for_multiple_nodes(tag, nodes, batch_size=DEFAULT_BATCH_SIZE):
"""Reevaluate a single tag for a multiple nodes.
Presumably this tag's expression has recently changed. Use `populate_tags`
when many nodes need reevaluating AND there are rack controllers available
to which to farm-out work. Use this only when many nodes need reevaluating
locally, i.e. when there are no rack controllers connected.
"""
# Same expression, multuple documents: compile expression with XPath.
xpath = etree.XPath(tag.definition, namespaces=tag_nsmap)
# The XML details documents can be large so work in batches.
for batch in gen_batches(nodes, batch_size):
probed_details = get_probed_details(batch)
probed_details_docs_by_node = {
node: merge_details(probed_details[node.system_id])
for node in batch
}
nodes_matching, nodes_nonmatching = classify(
partial(try_match_xpath, xpath, logger=maaslog),
probed_details_docs_by_node.items(),
)
tag.node_set.remove(*nodes_nonmatching)
tag.node_set.add(*nodes_matching)
示例10: test_DictCharWidget_renders_with_empty_string_as_input_data
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def test_DictCharWidget_renders_with_empty_string_as_input_data(self):
names = [factory.make_string(), factory.make_string()]
initials = []
labels = [factory.make_string(), factory.make_string()]
widget = DictCharWidget(
[widgets.TextInput, widgets.TextInput, widgets.CheckboxInput],
names,
initials,
labels,
skip_check=True,
)
name = factory.make_string()
html_widget = fromstring(
"<root>" + widget.render(name, "") + "</root>"
)
widget_names = XPath("fieldset/input/@name")(html_widget)
widget_labels = XPath("fieldset/label/text()")(html_widget)
expected_names = [
"%s_%s" % (name, widget_name) for widget_name in names
]
self.assertEqual(
[expected_names, labels], [widget_names, widget_labels]
)
示例11: filter_add
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def filter_add(self, xpath):
self.filters.append(ET.XPath(xpath))
示例12: group_by
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def group_by(self, xpath, required=False):
self.groups.append(ET.XPath(xpath))
if required:
self.filter_add(xpath)
示例13: makeXmlPageFromRaw
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def makeXmlPageFromRaw(xml):
""" Discard the metadata around a <page> element in <mediawiki> string"""
root = etree.XML(xml)
find = etree.XPath("//*[local-name() = 'page']")
# The tag will inherit the namespace, like:
# <page xmlns="http://www.mediawiki.org/xml/export-0.10/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
# FIXME: pretty_print doesn't seem to work, only adds a newline
return etree.tostring(find(root)[0], pretty_print=True)
示例14: final_attribute_name
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def final_attribute_name(xpath):
"""
Find the final text element of an xpath which we will assume is the name
of an attribute.
TODO: find a better and less error-prone way to do this!
"""
if type(xpath) == XPath: ## in case compiled:
pathstring = xpath.path
else:
pathstring = xpath
fragments = re.split("[/:@\(\)]+", pathstring)
return fragments[-1]
示例15: _make_xpath_builder
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import XPath [as 别名]
def _make_xpath_builder(self):
namespaces = {
'ds' : 'http://www.w3.org/2000/09/xmldsig#',
'md' : 'urn:oasis:names:tc:SAML:2.0:metadata',
'saml' : 'urn:oasis:names:tc:SAML:2.0:assertion',
'samlp': 'urn:oasis:names:tc:SAML:2.0:protocol'
}
def xpath_with_namespaces(xpath_str):
return etree.XPath(xpath_str, namespaces=namespaces)
return xpath_with_namespaces