From a8a9224b7f97e882cc8ec712323d8b86631e42e9 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Sat, 6 May 2017 14:43:41 -0400 Subject: Renamed convenience method to get_attribute_text. --- NEWS.txt | 2 +- bs4/element.py | 2 +- bs4/tests/test_tree.py | 2 +- doc/source/index.rst | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/NEWS.txt b/NEWS.txt index aacd025..daeec24 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -1,6 +1,6 @@ = 4.6.0 (Unreleased) = -* Added the `Tag.stringattr` method, which acts like `Tag.get` for +* Added the `Tag.get_attribute_text` method, which acts like `Tag.get` for getting the value of an attribute, but which joins attribute multi-values into a single string value. [bug=1678589] diff --git a/bs4/element.py b/bs4/element.py index 5462592..71cdb65 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -992,7 +992,7 @@ class Tag(PageElement): attribute.""" return self.attrs.get(key, default) - def string_attr(self, key, default=None): + def get_attribute_text(self, key, default=None): """The same as get(), but converts lists of values to strings.""" value = self.get(key, default) if isinstance(value, list): diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index f57255d..90a6d25 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -1288,7 +1288,7 @@ class TestCDAtaListAttributes(SoupTest): def test_attribute_values_joined_into_string_through_string_attr(self): soup = self.soup("") - self.assertEqual(b'foo bar', soup.a.string_attr('class')) + self.assertEqual('foo bar', soup.a.get_attribute_text('class')) def test_accept_charset(self): soup = self.soup('
') diff --git a/doc/source/index.rst b/doc/source/index.rst index 0d13e0a..7bee329 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -428,10 +428,10 @@ consolidated:: print(rel_soup.p) #

Back to the homepage

-You can use ```string_attr`` to get the value of any attribute as a +You can use ```get_attribute_text`` to get the value of any attribute as a string, whether or not it's a multi-valued atribute:: - css_soup.p.string_attr('class') + css_soup.p.get_attribute_text('class') # "body strikeout" If you parse a document as XML, there are no multi-valued attributes:: -- cgit v1.2.1