diff options
Diffstat (limited to 'beautifulsoup')
-rw-r--r-- | beautifulsoup/__init__.py | 7 | ||||
-rw-r--r-- | beautifulsoup/element.py | 17 |
2 files changed, 17 insertions, 7 deletions
diff --git a/beautifulsoup/__init__.py b/beautifulsoup/__init__.py index 8817164..4a7e18b 100644 --- a/beautifulsoup/__init__.py +++ b/beautifulsoup/__init__.py @@ -194,13 +194,6 @@ class BeautifulStoneSoup(Tag): def popTag(self): tag = self.tagStack.pop() - # Tags with just one string-owning child get the child as a - # 'string' property, so that soup.tag.string is shorthand for - # soup.tag.contents[0] - if len(self.currentTag.contents) == 1 and \ - isinstance(self.currentTag.contents[0], NavigableString): - self.currentTag.string = self.currentTag.contents[0] - #print "Pop", tag.name if self.tagStack: self.currentTag = self.tagStack[-1] 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 |