本文整理匯總了Python中lxml.etree方法的典型用法代碼示例。如果您正苦於以下問題:Python lxml.etree方法的具體用法?Python lxml.etree怎麽用?Python lxml.etree使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類lxml
的用法示例。
在下文中一共展示了lxml.etree方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: on_device_list_attached
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def on_device_list_attached(self, vm, event, **kwargs):
# pylint: disable=unused-argument,no-self-use
if not vm.is_running() or isinstance(vm, qubes.vm.adminvm.AdminVM):
return
xml_desc = lxml.etree.fromstring(vm.libvirt_domain.XMLDesc())
for hostdev in xml_desc.findall('devices/hostdev'):
if hostdev.get('type') != 'pci':
continue
address = hostdev.find('source/address')
bus = address.get('bus')[2:]
device = address.get('slot')[2:]
function = address.get('function')[2:]
ident = '{bus}_{device}.{function}'.format(
bus=bus,
device=device,
function=function,
)
yield (PCIDevice(vm.app.domains[0], ident), {})
示例2: rss_item_to_relevant_data
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def rss_item_to_relevant_data(self, item):
"""
Extract the relevant data from the given RSS item.
Args:
`item`:
A single item from the RSS feed. Such an
item is an element of a list obtained with a
`<lxml etree/html document>.xpath(...)` call
(see the source code of the _process_rss()
method).
Returns:
Some hashable object. It may be, for example, a
tuple or a string -- the exact type depends on the
implementation provided by a particular subclass
of BaseRSSCollector.
"""
raise NotImplementedError
示例3: update_api_keys
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def update_api_keys(self):
logger.debug("updating MLB api keys")
content = self.session.get(self.MLB_API_KEY_URL).text
parser = lxml.etree.HTMLParser()
data = lxml.etree.parse(StringIO(content), parser)
scripts = data.xpath(".//script")
for script in scripts:
if script.text and "apiKey" in script.text:
self._state.api_key = self.API_KEY_RE.search(script.text).groups()[0]
if script.text and "clientApiKey" in script.text:
self._state.client_api_key = self.CLIENT_API_KEY_RE.search(script.text).groups()[0]
logger.debug("updating Okta api keys")
content = self.session.get(self.MLB_OKTA_URL).text
self._state.okta_client_id = self.OKTA_CLIENT_ID_RE.search(content).groups()[0]
self.save()
示例4: is_logged_in
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def is_logged_in(self):
logged_in_url = "https://secure.mlb.com/account/login_register.jsp?flowId=registration.newsletter&c_id=mlb"
content = self.session.get(logged_in_url).text
parser = lxml.etree.HTMLParser()
data = lxml.etree.parse(io.StringIO(content), parser)
if "Login/Register" in data.xpath(".//title")[0].text:
return False
return True
示例5: doc
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def doc(self):
"""Returns a PyQuery object of the response's content"""
if hasattr(self, '_doc'):
return self._doc
elements = self.etree
doc = self._doc = PyQuery(elements)
doc.make_links_absolute(utils.text(self.url))
return doc
示例6: etree
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def etree(self):
"""Returns a lxml object of the response's content that can be selected by xpath"""
if not hasattr(self, '_elements'):
try:
parser = lxml.html.HTMLParser(encoding=self.encoding)
self._elements = lxml.html.fromstring(self.content, parser=parser)
except LookupError:
# lxml would raise LookupError when encoding not supported
# try fromstring without encoding instead.
# on windows, unicode is not availabe as encoding for lxml
self._elements = lxml.html.fromstring(self.content)
if isinstance(self._elements, lxml.etree._ElementTree):
self._elements = self._elements.getroot()
return self._elements
示例7: evaluate_criteria
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def evaluate_criteria(cls, prp, content_etree, criteria):
"""
Evaluates the criteria in a query. Note that criteria can have
child criteria (which will cause recursion) and child criterion.
Arguments:
content_etree - an lxml etree to evaluate
criteria - the criteria to evaluate against the etree
Returns:
True or False, indicating whether the content_etree
matches the criteria
"""
for child_criteria in criteria.criteria:
value = cls.evaluate_criteria(prp, content_etree, child_criteria)
if value is True and criteria.operator == tdq.OP_OR:
return True
elif value is False and criteria.operator == tdq.OP_AND:
return False
else: # Don't know anything for sure yet
pass
for criterion in criteria.criterion:
value = cls.evaluate_criterion(prp, content_etree, criterion)
# TODO: Is there a way to keep this DRY?
if value is True and criteria.operator == tdq.OP_OR:
return True
elif value is False and criteria.operator == tdq.OP_AND:
return False
else: # Don't know anything for sure yet
pass
return criteria.operator == tdq.OP_AND
示例8: evaluate_criterion
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def evaluate_criterion(cls, prp, content_etree, criterion):
"""
Evaluates the criterion in a query by turning the Criterion into an XPath and
evaluating it against the content_etree
Arguments:
content_etree - an lxml etree to evaluate
criterion - the criterion to evaluate against the etree
Returns:
True or False, indicating whether the content_etree
matches the criterion
"""
xpath, nsmap = cls.get_xpath(prp, criterion)
# print xpath
matches = content_etree.xpath(xpath, namespaces=nsmap)
# XPath results can be a boolean (True, False) or
# a NodeSet
if matches in (True, False): # The result is boolean, take it literally
result = matches
else: # The result is a NodeSet. The Criterion is True iff there are >0 resulting nodes
result = len(matches) > 0
if criterion.negate:
return not result
return result
示例9: description
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def description(self):
if self._description is None:
hostdev_details = \
self.backend_domain.app.vmm.libvirt_conn.nodeDeviceLookupByName(
self.libvirt_name
)
self._description = _device_desc(lxml.etree.fromstring(
hostdev_details.XMLDesc()))
return self._description
示例10: on_device_list_pci
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def on_device_list_pci(self, vm, event):
# pylint: disable=unused-argument,no-self-use
# only dom0 expose PCI devices
if vm.qid != 0:
return
for dev in vm.app.vmm.libvirt_conn.listAllDevices():
if 'pci' not in dev.listCaps():
continue
xml_desc = lxml.etree.fromstring(dev.XMLDesc())
libvirt_name = xml_desc.findtext('name')
yield PCIDevice(vm, None, libvirt_name=libvirt_name)
示例11: _process_rss
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def _process_rss(self, result):
try:
document = lxml.etree.fromstring(result)
except lxml.etree.XMLSyntaxError:
document = lxml.html.fromstring(result)
data_row_xpath = "//item"
rows = document.xpath(data_row_xpath)
return set(map(self.rss_item_to_relevant_data, rows))
示例12: get_xml
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def get_xml(self):
report = Element("report")
report.set("name", self.app_name)
groups = Utils2.get_groupped_classes(self.smalitree)
for g in groups:
package = SubElement(report,"package")
package.set("name", Utils2.get_package_name(g[0].name))
for cl in g:
if (cl.is_coverable()):
self.add_xml_class(package, cl)
return etree.tostring(report, pretty_print=True)
示例13: _prepare_documents
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def _prepare_documents(cls):
if cls._schema is None:
cls._schema = lxml.etree.fromstring(cls.SCHEMA)
cls._avalue = lxml.etree.fromstring(cls.AVALUE)
cls._pattern = cls._schema[0][0][0][0]
示例14: __init__
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def __init__(self, spec, pos, invert_match):
self._prepare_documents()
self.spec = spec
self.pos = pos
self.invert_match = invert_match
self._pattern.set('value', spec)
try:
self.schema = lxml.etree.XMLSchema(etree=self._schema)
except lxml.etree.XMLSchemaParseError as err:
self.schema = None
self.error = err
else:
self.error = None
示例15: _get_schema
# 需要導入模塊: import lxml [as 別名]
# 或者: from lxml import etree [as 別名]
def _get_schema():
# file_name = of_config.OF_CONFIG_1_0_XSD
# file_name = of_config.OF_CONFIG_1_1_XSD
file_name = of_config.OF_CONFIG_1_1_1_XSD
return lxml.etree.XMLSchema(file=file_name)