diff options
| author | Leonard Richardson <leonardr@segfault.org> | 2017-05-06 14:32:49 -0400 |
|---|---|---|
| committer | Leonard Richardson <leonardr@segfault.org> | 2017-05-06 14:32:49 -0400 |
| commit | 13ec3c3f3010820be225094cc06862cc4b3b2944 (patch) | |
| tree | 7dfd405d9fe4c415f95bab42daf098d0bdb91da7 /bs4/element.py | |
| parent | c556b8a6b42843fac40c55459aa5c494e2798349 (diff) | |
Added the method, which acts like for
getting the value of an attribute, but which joins attribute
multi-values into a single string value. [bug=1678589]
Diffstat (limited to 'bs4/element.py')
| -rw-r--r-- | bs4/element.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/bs4/element.py b/bs4/element.py index 115ab24..5462592 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -131,8 +131,8 @@ class PageElement(object): # to methods like encode() and prettify(): # # "html" - All Unicode characters with corresponding HTML entities - # are converted to those entities on output. - # "minimal" - Bare ampersands and angle brackets are converted to + # are converted to those entities on output. + # "minimal" - Bare ampersands and angle brackets are converted to # XML entities: & < > # None - The null formatter. Unicode characters are never # converted to entities. This is not recommended, but it's @@ -992,6 +992,13 @@ class Tag(PageElement): attribute.""" return self.attrs.get(key, default) + def string_attr(self, key, default=None): + """The same as get(), but converts lists of values to strings.""" + value = self.get(key, default) + if isinstance(value, list): + value = " ".join(value) + return value + def has_attr(self, key): return key in self.attrs |
