[HTML] How to get text of attribute?
Hello, My XPath skills being what they are… in an HTML file, I can't figure out how to grab the text of the second attribute in a meta element: ================== with open("input.html") as tempfile: parser = et.HTMLParser(remove_blank_text=True,recover=True) currfile_tree = et.parse(tempfile, parser=parser) currfile_root = currfile_tree.getroot() """ <head> <title>Blah</title> <meta name="description" content="HERE"/> </head>' """ description=currfile_root.xpath("//meta[@name='description' and @content]") ================== Any idea? Thank you.
For others' benefit: ======= description = tree.xpath("//meta[@name='description']/@content") print(description[0]) =======
Maybe even easier: string() in the XPATH: === description = tree.xpath('string(//meta[@name="description"]/@content)') print(description) === best, /PA On Fri, 27 May 2022 at 19:03, <codecomplete@free.fr> wrote:
For others' benefit:
======= description = tree.xpath("//meta[@name='description']/@content") print(description[0]) ======= _______________________________________________ lxml - The Python XML Toolkit mailing list -- lxml@python.org To unsubscribe send an email to lxml-leave@python.org https://mail.python.org/mailman3/lists/lxml.python.org/ Member address: paaguti@gmail.com
-- Fragen sind nicht da um beantwortet zu werden, Fragen sind da um gestellt zu werden Georg Kreisler Headaches with a Juju log: unit-basic-16: 09:17:36 WARNING juju.worker.uniter.operation we should run a leader-deposed hook here, but we can't yet
Even better! Thanks. On 28/05/2022 12:28, Pedro Andres Aranda Gutierrez wrote:
Maybe even easier: string() in the XPATH:
=== description = tree.xpath('string(//meta[@name="description"]/@content)') print(description) ===
best, /PA
On Fri, 27 May 2022 at 19:03, <codecomplete@free.fr> wrote:
For others' benefit:
======= description = tree.xpath("//meta[@name='description']/@content") print(description[0]) =======
participants (3)
-
codecomplete@free.fr
-
Gilles
-
Pedro Andres Aranda Gutierrez