Python Forum
[SOLVED] [ElementTree] Grab text in attributes?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [ElementTree] Grab text in attributes?
#1
Hello,

I'm not very good at XPath, and am a bit lost at the syntax to 1) find an element based on the value of its first attribute, and grab the text of the second attribute in an HTML file:

<meta name="description" content="Blah"/>
<meta name="keywords" content="blah"/>
<meta name="classification" content="other"/>

description = root.find('./head/meta[@description]')
print(description.text)
Thank you.

--
Edit: Getting closer

description=root.xpath("//meta[@name='description' and @content]")
#BAD print(description.text) #'list' object has no attribute 'text'
Reply
#2
you didn't say what method you arte using to find description
with selenium, use:
description = browser.find_element(by=By.XPATH, value="//meta[@name='description']").text
note you may have to replace browser with 'driver' or whatever you opened selenium with.
Reply
#3
I would not use ElementTree for parsing html,look at Web-Scraping part-1
from bs4 import BeautifulSoup

data = '''\
<html>
  <meta name="description" content="Blah"/>
  <meta name="keywords" content="blah"/>
  <meta name="classification" content="other"/>
<html>'''

soup = BeautifulSoup(data, 'lxml')
tag = soup.find('meta', {'name': 'keywords'})
>>> tag
<meta content="blah" name="keywords"/>
>>> tag.attrs
{'content': 'blah', 'name': 'keywords'}
>>> tag.attrs.get('content')
'blah'
If want to use XPath i would use lxml.
from lxml import etree

data = '''\
<html>
  <meta name="description" content="Blah"/>
  <meta name="keywords" content="blah"/>
  <meta name="classification" content="other"/>
</html>'''

tree = etree.fromstring(data)
tag = tree.xpath("//meta[@name='classification']/@content")
print(tag[0])
Output:
other
Reply
#4
Thanks much. I forgot to say I'm actually using lxml, and the code above solved it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  scan network and grab details of hosts found robertkwild 5 1,351 Aug-07-2024, 05:21 PM
Last Post: Larz60+
Question [PyMuPDF] Grab all strings of a given size? Winfried 3 1,604 Dec-26-2023, 07:39 AM
Last Post: Pedroski55
  Can't stop keyboard listener to grab chars typed inside CTk window Valjean 9 3,604 Sep-25-2023, 08:07 PM
Last Post: deanhystad
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 1,932 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  [SOLVED] [BeautifulSoup] How to get this text? Winfried 6 3,162 Aug-17-2022, 03:58 PM
Last Post: Winfried
  Delete empty text files [SOLVED] AlphaInc 5 3,184 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  [ElementTree] Insert big block of HTML? Winfried 0 1,705 May-12-2022, 07:08 AM
Last Post: Winfried
  ElementTree get attribute value part of string paulo79 1 3,792 Apr-05-2022, 09:13 PM
Last Post: deanhystad
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,938 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Screen capture opencv - grab error Kalman15 1 2,776 Jan-27-2022, 12:22 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020