Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

DOC mention gotcha with `foo::text` selector and empty `foo` elements

Mikhail Korobovffbd33edac03
diff --git a/docs/topics/selectors.rst b/docs/topics/selectors.rst
index 68913c697..9dced7473 100644
--- a/docs/topics/selectors.rst
+++ b/docs/topics/selectors.rst
@@ -279,6 +279,19 @@ Examples:
'Name: My image 5 ',
'\n ']
+* ``foo::text`` returns no results if ``foo`` element exists, but contains
+ no text (i.e. text is empty)::
+
+ >>> response.css('img::text').getall()
+ []
+
+ This means ``.css('foo::text').get()`` could return None even if an element
+ exists. Use ``default=''`` if you always want a string::
+
+ >>> response.css('img::text').get()
+ >>> response.css('img::text').get(default='')
+ ''
+
* ``a::attr(href)`` selects the *href* attribute value of descendant links::
>>> response.css('a::attr(href)').getall()
@@ -288,15 +301,14 @@ Examples:
'image4.html',
'image5.html']
+.. note::
+ See also: :ref:`selecting-attributes`.
+
.. note::
You cannot chain these pseudo-elements. But in practice it would not
make much sense: text nodes do not have attributes, and attribute values
are string values already and do not have children nodes.
-.. note::
- See also: :ref:`selecting-attributes`.
-
-
.. _CSS Selectors: https://www.w3.org/TR/css3-selectors/#selectors
.. _topics-selectors-nesting-selectors: