summaryrefslogtreecommitdiff
path: root/beautifulsoup/element.py
diff options
context:
space:
mode:
authorLeonard Richardson <leonard.richardson@canonical.com>2011-01-28 21:08:42 -0500
committerLeonard Richardson <leonard.richardson@canonical.com>2011-01-28 21:08:42 -0500
commit7a6f07b1650eaba61d4c669feb5565cf443b2531 (patch)
treed00ed1687710ffd18929ce44319330900a84c67a /beautifulsoup/element.py
parent58a9d28ced3c133dc945266024e629335af16196 (diff)
parentf0b1b4f0f3c2ce4e140ba01a6690ddb16bf0fb17 (diff)
Made .string a dynamic property.
Diffstat (limited to 'beautifulsoup/element.py')
-rw-r--r--beautifulsoup/element.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/beautifulsoup/element.py b/beautifulsoup/element.py
index 7649b4c..bd9bcbf 100644
--- a/beautifulsoup/element.py
+++ b/beautifulsoup/element.py
@@ -434,6 +434,23 @@ class Tag(PageElement, Entities):
else:
self.attrs = map(convert, attrs)
+ @property
+ def string(self):
+ """Convenience property to get the single string within this tag.
+
+ :Return: If this tag has a single string child, return value
+ is that string. If this tag has no children, or more than one
+ child, return value is None. If this tag has one child tag,
+ return value is the 'string' attribute of the child tag,
+ recursively.
+ """
+ if len(self.contents) != 1:
+ return None
+ child = self.contents[0]
+ if isinstance(child, NavigableString):
+ return child
+ return child.string
+
def get(self, key, default=None):
"""Returns the value of the 'key' attribute for the tag, or
the value given for 'default' if it doesn't have that