diff --git a/parsel/selector.py b/parsel/selector.py index 7b9bdc51..1180cabe 100644 --- a/parsel/selector.py +++ b/parsel/selector.py @@ -137,6 +137,16 @@ def extract_first(self, default=None): return default get = extract_first + @property + def attrib(self): + """Return the attributes dictionary for the first element. + If the list is empty, return an empty dict. + """ + for x in self: + return x.attrib + else: + return {} + class Selector(object): """ @@ -324,6 +334,12 @@ def remove_namespaces(self): # remove namespace declarations etree.cleanup_namespaces(self.root) + @property + def attrib(self): + """Return the attributes dictionary for underlying element. + """ + return dict(self.root.attrib) + def __bool__(self): """ Return ``True`` if there is any real content selected or ``False`` diff --git a/tests/test_selector.py b/tests/test_selector.py index dcac22b9..1d3250a9 100644 --- a/tests/test_selector.py +++ b/tests/test_selector.py @@ -86,6 +86,34 @@ def test_simple_selection_with_variables_escape_friendly(self): lng=lt)], [u'a']) + def test_accessing_attributes(self): + body = u""" + +
+