本文整理汇总了Python中lxml.etree.ElementTree.find方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.find方法的具体用法?Python ElementTree.find怎么用?Python ElementTree.find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lxml.etree.ElementTree
的用法示例。
在下文中一共展示了ElementTree.find方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Parser
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
class Parser(object):
def __init__(self, template_name):
self.template_name = template_name
self.file_path = join(TEMPLATE_PATH, self.template_name)
self.doc = ElementTree(file = self.file_path)
def _onepage_metadata(self):
self.onepage_dict = {}
page = {'page': int(self.doc.find('OnePage').attrib['page'])}
self.onepage_dict.update(page)
for elem in self.doc.iterfind('OnePage/metadata'):
for sub_elem in list(elem):
key = elem.attrib['id'] + '_' + sub_elem.tag
value = sub_elem.text.encode('utf-8')
if value.isdigit():
value = int(value)
elif '\\n' in value:
value = value.replace('\\n', '\n').split(',')
else:
value = value.split(',')
self.onepage_dict.update({key: value})
return self.onepage_dict
def _variouspages_metadata(self):
self.variouspages_dict = {}
various_pages = self.doc.find('VariousPages')
if various_pages is not None:
first_page = int(self.doc.find('VariousPages').attrib['startpage'])
end_page = int(self.doc.find('VariousPages').attrib['endpage'])
pages_parse = [first_page, end_page]
pages = {'pages': pages_parse}
self.variouspages_dict.update(pages)
for elem in self.doc.iterfind('VariousPages/metadata'):
for sub_elem in list(elem):
key = elem.attrib['id'] + '_' + sub_elem.tag
value = sub_elem.text.encode('utf-8')
if value.isdigit():
value = int(value)
elif '\\n' in value:
value = value.replace('\\n', '\n').split(',')
else:
value = value.split(',')
self.variouspages_dict.update({key: value})
return self.variouspages_dict
def xml_template_metadata(self):
onepage_metadata = self._onepage_metadata()
variouspages_metadata = self._variouspages_metadata()
onepage_metadata.update(variouspages_metadata)
all_template_metadata = onepage_metadata
return all_template_metadata
示例2: produce_settings_file
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def produce_settings_file(self, printer):
printer_name = printer['name']
tree = ElementTree()
tree.parse(StringIO(XML_TEMPLATE.replace('\n', '').replace('\r', '').replace('\t', '')))
name_node = tree.find('name')
name_node.text = 'AirPrint %s @ %%h' % printer_name
service_node = tree.find('service')
port_node = service_node.find('port')
port_node.text = '%d' % printer['port']
host = printer['host']
if host:
if self.dnsdomain:
pair = host.rsplit('.', 1)
if len(pair) > 1:
host = '.'.join((pair[0], self.dnsdomain))
service_node.append(self.new_node('host-name', host))
txt = printer['txt']
for key in txt:
if self.adminurl or key != 'adminurl':
service_node.append(self.new_txtrecord_node('%s=%s' % (key, txt[key])))
source = printer['SOURCE'] if printer.has_key('SOURCE') else ''
fname = '%s%s%s.service' % (self.prefix, '%s-' % source if len(source) > 0 else '', printer_name)
if self.directory:
fname = os.path.join(self.directory, fname)
f = open(fname, 'w')
if etree:
tree.write(f, pretty_print=True, xml_declaration=True, encoding="UTF-8")
else:
xmlstr = tostring(tree.getroot())
doc = parseString(xmlstr)
dt= minidom.getDOMImplementation('').createDocumentType('service-group', None, 'avahi-service.dtd')
doc.insertBefore(dt, doc.documentElement)
doc.writexml(f)
f.close()
if self.verbose:
src = source if len(source) > 0 else 'unknown'
sys.stderr.write('Created from %s: %s%s' % (src, fname, os.linesep))
示例3: getFirmwares
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def getFirmwares(self):
# print 'refreshing'
url = urlparse(self.url)
params = '<?xml version="1.0" encoding="utf-8" ?>\n' +\
'<D:propfind xmlns:D="DAV:">\n'+\
'<D:allprop/>\n' +\
'</D:propfind>'
base64string = base64.encodestring('%s:%s' % (self.username, self.password))[:-1]
authheader = "Basic %s" % base64string
headers = {
"Content-Type" : "application/xml; charset=\"utf-8\"",
"Authorization": authheader,
"Depth" : "1"}
conn = httplib.HTTPConnection(url.netloc)
conn.request("PROPFIND", url.path, params, headers)
response = conn.getresponse()
data = response.read()
conn.close()
if "Authorization Required" in data:
self.authenticated = False
return
data_elements = HTML(data)
xml_etree = ElementTree(data_elements)
all_response_elements = xml_etree.findall("//response")
self.folders = []
for response in all_response_elements:
resp_tree = ElementTree(response)
if resp_tree.find('//collection') is None:
uri = resp_tree.find("//href").text
getcontentlength = getattr(resp_tree.find('//getcontentlength'), 'text', None)
getlastmodified = getattr(resp_tree.find('//getlastmodified'), 'text', None)
name = os.path.basename(uri)
self.firmwares[name] = FirmWare(name,url.scheme+'://'+url.netloc+uri,getlastmodified,getcontentlength)
else:
self.folders.append(resp_tree.find("//href").text)
示例4: __init__
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def __init__(self, name):
self.name = name
tree = ElementTree(file = name)
labels = tuple((elt.tag.strip(), elt.text.strip()) for elt in tree.find("labels"))
self.labels = tuple(pair[0] for pair in labels)
self.descrs = dict(labels)
self.date = tree.getroot().get("date")
for elt in tree.findall("validation_status"):
status = elt.get("status")
uri = elt.text.strip()
if status.startswith("rsync_transfer_") or elt.get("generation") != "current":
continue
if uri not in self:
self[uri] = Object(self, uri)
self[uri].add(status)
示例5: __init__
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
class H248Message:
def __init__(self, msg):
self.msg = msg
self.xml = self.msg.asXML()
self.tree = ElementTree(fromstring(self.xml))
def xpath(self, path):
elem = self.tree.find(path)
if elem is None:
raise InvalidXPath()
return elem.text
def abstract(self):
return self.xml
def __str__(self):
return self.xml
示例6: timeandweather
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def timeandweather():
# If this ever dies, there's always Microsoft:
# http://weather.service.msn.com/find.aspx?outputview=search&src=Windows7&weasearchstr=02139&weadegreetype=F&culture=en-US
f = urllib.urlopen("http://weather.yahooapis.com/forecastrss?w=12758738&u=f")
e = ElementTree(file=f)
cond = e.find(".//{http://xml.weather.yahoo.com/ns/rss/1.0}condition")
img = e.findtext(".//item/description")
img = parse(StringIO("<doc>%s</doc>" % img)).find(".//img").get("src")
# cond.get("text") is nice too
return '<table class="weather" align="center" cellspacing="20"><tr><td>' + \
"<h2>"+time.strftime("%l:%M %p<br />%b %e")+"</h2>" + \
"</td><td>" + \
'<img src="%s" /><h2>%s°F</h2>' % (img, cond.get("temp")) + \
"</td></tr></table>"
示例7: __init__
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def __init__(self, name):
self.name = name
tree = ElementTree(file = name)
self.labels = [elt.tag.strip() for elt in tree.find("labels")]
for elt in tree.findall("validation_status"):
generation = elt.get("generation")
status = elt.get("status")
uri = elt.text.strip()
if not show_rsync_transfer and status.startswith("rsync_transfer_"):
continue
if show_backup_generation:
key = (uri, generation)
elif generation == "backup":
continue
else:
key = uri
if key not in self:
self[key] = Object(self, uri, generation)
self[key].add(status)
示例8: get_committees
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def get_committees(self, term, chamber, laws_year):
committee_list = []
committee_list_url = self.committee_list_url_template % laws_year
list_page = ElementTree(lxml.html.fromstring(self.urlopen(committee_list_url)))
com_select = list_page.find('//select[@name="P_COM_NM"]')
for option in com_select.findall("option"):
if option.text:
committee_url = self.committee_url_template % (laws_year,
urllib.quote(option.text.strip()))
c_chamber, name = option.text.split(" ", 1)
c_chamber = c_chamber[1]
if (('H' == c_chamber and 'lower' == chamber) or
('S' == c_chamber and 'upper' == chamber)):
# committee = Committee(term['name'], chamber, name)
committee = Committee(chamber, name)
committee.add_source(committee_url)
committee_list.append(committee)
return committee_list
示例9: getterm
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def getterm(request, lang, code):
params = urllib.urlencode({
'tree_id': code or '',
'lang': lang,
})
resource = urllib.urlopen(settings.DECS_SERVICE, params)
tree = ElementTree()
tree.parse(resource)
result = tree.find("decsws_response/tree/self/term_list/term")
if result is None:
result = tree.findall('decsws_response/tree/term_list[@lang="%s"]/term' % lang)
json = '[%s]' % ','.join((JSON_TERM % (r.text.capitalize(),r.attrib['tree_id']) for r in result))
else:
descriptors = tree.findall('decsws_response/record_list/record/descriptor_list/descriptor')
description = ','.join(['"%s":"%s"'%(d.attrib['lang'],d.text) for d in descriptors])
json = '[%s]' % (JSON_MULTILINGUAL_TERM % (description,result.attrib['tree_id']))
return HttpResponse(json, mimetype='application/json')
示例10: load
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def load(self, path):
parser = ElementTree(file=open(path, 'r'))
circles = parser.find('circles')
outline = parser.find('outline')
bb = outline.find('bounding_box')
bs = bb.find('width'), bb.find('height')
w, h = [float(b.text) for b in bs]
use_label = parser.find('use_label')
if use_label is not None:
use_label = to_bool(use_label.text.strip())
else:
use_label = True
data = ArrayPlotData()
p = Plot(data=data, padding=100)
p.x_grid.visible = False
p.y_grid.visible = False
p.x_axis.visible = False
p.y_axis.visible = False
p.x_axis.title = 'X cm'
p.y_axis.title = 'Y cm'
# font = 'modern 22'
# p.x_axis.title_font = font
# p.x_axis.tick_label_font = font
# p.y_axis.title_font = font
# p.y_axis.tick_label_font = font
# p.x_axis_visible = False
# p.y_axis_visible = False
p.index_range.low_setting = -w / 2
p.index_range.high_setting = w / 2
p.value_range.low_setting = -h / 2
p.value_range.high_setting = h / 2
thetas = linspace(0, 2 * pi)
radius = circles.find('radius').text
radius = float(radius)
face_color = circles.find('face_color')
if face_color is not None:
face_color = face_color.text
else:
face_color = 'white'
labels = []
for i, pp in enumerate(circles.findall('point')):
x, y, l = pp.find('x').text, pp.find('y').text, pp.find('label').text
# print i, pp, x, y
# load hole specific attrs
r = pp.find('radius')
if r is None:
r = radius
else:
r = float(r.text)
fc = pp.find('face_color')
if fc is None:
fc = face_color
else:
fc = fc.text
x, y = list(map(float, (x, y)))
xs = x + r * sin(thetas)
ys = y + r * cos(thetas)
xn, yn = 'px{:03d}'.format(i), 'py{:03d}'.format(i)
data.set_data(xn, xs)
data.set_data(yn, ys)
plot = p.plot((xn, yn),
face_color=fc,
type='polygon')[0]
labels.append((x, y, l))
# if use_label:
# label = myDataLabel(component=plot,
# data_point=(x, y),
# label_text=l,
# bgcolor='transparent')
# plot.overlays.append(label)
if use_label:
p.overlays.append(LabelsOverlay(component=plot, labels=labels))
self.container.add(p)
self.container.invalidate_and_redraw()
示例11: open
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
# encode the payload explicitly as UTF-8, since it contains chinese characters
page_source = page_source.encode(encoding='UTF-8',errors='strict')
html_file = open(html_filename, 'wb')
html_file.write(page_source)
html_file.close()
print "Html retrieved successfully"
# decode the payload explicitly as UTF-8
html_content = open(html_filename).read()
if hasattr(html_content, 'decode'):
html_content = html_content.decode('utf-8')
# search in the tree to find the elements containing the image url and the score
tree = ElementTree(lxml.html.document_fromstring(html_content))
element = tree.find("//div[@id='ColumnContainer']")
children = list(element)
print "Number of images:", len(children)
image_urls = []
scores = []
for child in children:
image_element = child.find("./a")
score_element = child.find(".//div[@class='d-name']")
image_urls.append(image_element.get('href'))
text_contents = score_element.text_content().split()
scores.append(text_contents.pop(-1))
# dump the URLs to a file
images_filename = os.path.join(images_folder, 'all-images-url.pkl')
if not os.path.exists(images_filename):
images_file = open(images_filename, 'wb')
示例12: load
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def load(self, path):
parser = ElementTree(file=open(path, 'r'))
circles = parser.find('circles')
outline = parser.find('outline')
bb = outline.find('bounding_box')
bs = bb.find('width'), bb.find('height')
w, h = map(lambda b:float(b.text), bs)
data = ArrayPlotData()
p = Plot(data=data)
p.x_grid.visible = False
p.y_grid.visible = False
p.index_range.low_setting = -w / 2
p.index_range.high_setting = w / 2
p.value_range.low_setting = -h / 2
p.value_range.high_setting = h / 2
thetas = linspace(0, 2 * pi)
radius = circles.find('radius').text
radius = float(radius)
face_color = circles.find('face_color')
if face_color is not None:
face_color = face_color.text
else:
face_color = 'white'
for i, pp in enumerate(circles.findall('point')):
x, y, l = pp.find('x').text, pp.find('y').text, pp.find('label').text
# load hole specific attrs
r = pp.find('radius')
if r is None:
r = radius
else:
r = float(r.text)
fc = pp.find('face_color')
if fc is None:
fc = face_color
else:
fc = fc.text
x, y = map(float, (x, y))
xs = x + r * sin(thetas)
ys = y + r * cos(thetas)
xn, yn = 'px{:03n}'.format(i), 'py{:03n}'.format(i)
data.set_data(xn, xs)
data.set_data(yn, ys)
plot = p.plot((xn, yn),
face_color=fc,
type='polygon',
)[0]
label = myDataLabel(component=plot,
data_point=(x, y),
label_text=l,
bgcolor=fc
)
plot.overlays.append(label)
self.container.add(p)
示例13: parse
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def parse(self, data):
""" Parse a webdav reply. Retrieve any resources as objects
and return them as a list.
:param data: The webdav reply to parse
:type data: String
:return: self.response_objects
"""
data_elements = HTML(data)
xml_etree = ElementTree(data_elements)
all_response_elements = xml_etree.findall("//response")
for response in all_response_elements:
new_response = Response()
resp_tree = ElementTree(response)
new_response.href = resp_tree.find('//href').text
if resp_tree.find('//collection') is not None:
new_response.resourcetype = 'collection'
else:
new_response.resourcetype = 'resource'
new_response.executable = getattr(
resp_tree.find('//executable'), 'text', None)
new_response.creationdate = getattr(
resp_tree.find('//creationdate'), 'text', None)
new_response.getcontentlength = getattr(
resp_tree.find('//getcontentlength'), 'text', None)
new_response.getlastmodified = getattr(
resp_tree.find('//getlastmodified'), 'text', None)
new_response.getetag = getattr(
resp_tree.find('//getetag'), 'text', None)
new_response.getcontenttype = getattr(
resp_tree.find('//getcontenttype'), 'text', None)
new_response.status = getattr(
resp_tree.find('//status'), 'text', None)
# Now we have the properties that are easy to get,
# lets get the lock information
lock_tree = resp_tree.findall('//lockentry')
for lock in lock_tree:
lock_tree = ElementTree(lock)
lock_obj = Lock()
lock_obj.locktype = lock_tree.find(
'//locktype').getchildren()[-1].tag
lock_obj.lockscope = lock_tree.find(
'//lockscope').getchildren()[-1].tag
new_response.locks.append(lock_obj)
self.response_objects.append(new_response)
return self.response_objects
示例14: generate
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def generate(self):
if not self.host:
conn = cups.Connection()
else:
if not self.port:
self.port = 631
conn = cups.Connection(self.host, self.port)
printers = conn.getPrinters()
for p, v in printers.items():
if v['printer-is-shared']:
attrs = conn.getPrinterAttributes(p)
uri = urlparse.urlparse(v['printer-uri-supported'])
tree = ElementTree()
tree.parse(StringIO(XML_TEMPLATE.replace('\n', '').replace('\r', '').replace('\t', '')))
name = tree.find('name')
name.text = 'AirPrint %s @ %%h' % (p)
service = tree.find('service')
port = service.find('port')
port_no = None
if hasattr(uri, 'port'):
port_no = uri.port
if not port_no:
port_no = self.port
if not port_no:
port_no = cups.getPort()
port.text = '%d' % port_no
if hasattr(uri, 'path'):
rp = uri.path
else:
rp = uri[2]
re_match = re.match(r'^//(.*):(\d+)(/.*)', rp)
if re_match:
rp = re_match.group(3)
#Remove leading slashes from path
#TODO XXX FIXME I'm worried this will match broken urlparse
#results as well (for instance if they don't include a port)
#the xml would be malform'd either way
rp = re.sub(r'^/+', '', rp)
path = Element('txt-record')
path.text = 'rp=%s' % (rp)
service.append(path)
desc = Element('txt-record')
desc.text = 'note=%s' % (v['printer-info'])
service.append(desc)
product = Element('txt-record')
product.text = 'product=(GPL Ghostscript)'
service.append(product)
state = Element('txt-record')
state.text = 'printer-state=%s' % (v['printer-state'])
service.append(state)
ptype = Element('txt-record')
ptype.text = 'printer-type=%s' % (hex(v['printer-type']))
service.append(ptype)
pdl = Element('txt-record')
fmts = []
defer = []
for a in attrs['document-format-supported']:
if a in DOCUMENT_TYPES:
if DOCUMENT_TYPES[a]:
fmts.append(a)
else:
defer.append(a)
if 'image/urf' not in fmts:
sys.stderr.write('image/urf is not in mime types, %s may not be available on ios6 (see https://github.com/tjfontaine/airprint-generate/issues/5)%s' % (p, os.linesep))
fmts = ','.join(fmts+defer)
dropped = []
# TODO XXX FIXME all fields should be checked for 255 limit
while len('pdl=%s' % (fmts)) >= 255:
(fmts, drop) = fmts.rsplit(',', 1)
dropped.append(drop)
if len(dropped) and self.verbose:
sys.stderr.write('%s Losing support for: %s%s' % (p, ','.join(dropped), os.linesep))
pdl.text = 'pdl=%s' % (fmts)
service.append(pdl)
if self.adminurl:
admin = Element('txt-record')
admin.text = 'adminurl=%s' % (v['printer-uri-supported'])
#.........这里部分代码省略.........
示例15: genPrinterService
# 需要导入模块: from lxml.etree import ElementTree [as 别名]
# 或者: from lxml.etree.ElementTree import find [as 别名]
def genPrinterService(p,v,airprint):
attrs = conn.getPrinterAttributes(p)
uri = urlparse.urlparse(v['printer-uri-supported'])
tree = ElementTree()
tree.parse(StringIO(SERVICE_TEMPLATE.replace('\n', '').replace('\r', '').replace('\t', '')))
name = tree.find('name')
name.text = '%s @ %%h' % (p if not airprint else 'AirPrint ' + p)
service = tree.find('service')
# Add subtype to AirPrint service definitions
if airprint:
path = Element('subtype')
path.text = "_universal._sub._ipp._tcp"
service.append(path)
port = service.find('port')
port_no = None
if hasattr(uri, 'port'): port_no = uri.port
if not port_no: port_no = self.port
if not port_no: port_no = cups.getPort()
port.text = '%d' % port_no
if hasattr(uri, 'path'): rp = uri.path
else: rp = uri[2]
re_match = re.match(r'^//(.*):(\d+)(/.*)', rp)
if re_match: rp = re_match.group(3)
#Remove leading slashes from path
#TODO XXX FIXME I'm worried this will match broken urlparse
#results as well (for instance if they don't include a port)
#the xml would be malform'd either way
rp = re.sub(r'^/+', '', rp)
path = Element('txt-record')
path.text = 'rp=%s' % (rp)
service.append(path)
# This is reported to Apple products as the location
if v['printer-location'] != '':
desc = Element('txt-record')
desc.text = 'note=%s' % (v['printer-location'])
service.append(desc)
# AirPrint and Non-AirPrint specific
if airprint:
product = Element('txt-record')
product.text = 'product=(GPL Ghostscript)'
service.append(product)
else:
product = Element('txt-record')
product.text = 'product=(%s)' %(v['printer-make-and-model'])
service.append(product)
# Add name
ptype = Element('txt-record')
ptype.text = 'ty=%s' %(v['printer-make-and-model'])
# USB manufacturer and model
if usbdevs.get(v['device-uri'], None) is not None:
usbdev = usbdevs.get(v['device-uri']).get('device-id',None)
if usbdev is not None:
manuf = None
model = None
# Tokenize values
kvs = usbdev.split(';')
for kv in kvs:
if not kv.find(':') > 0: continue
[k,val] = kv.split(':')
if k == 'MFG': manuf = val
elif k == 'MODEL' or k == 'MDL': model = val
if manuf is not None and model is not None:
man = Element('txt-record')
man.text = 'usb_MFG=%s' %(manuf)
service.append(man)
man = Element('txt-record')
man.text = 'usb_MDL=%s' %(model)
service.append(man)
# Color
if attrs.get('color-supported', True):
extTag = Element('txt-record')
extTag.text = 'Color=T'
service.append(extTag)
# Duplex
#.........这里部分代码省略.........