Skip to content

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

Response.follow_all: return empty generators for empty sequences

Eugenio Lacuesta8d30dc08882e
diff --git a/scrapy/http/response/text.py b/scrapy/http/response/text.py
index 33a485328..2f0f3820c 100644
--- a/scrapy/http/response/text.py
+++ b/scrapy/http/response/text.py
@@ -188,9 +188,11 @@ class TextResponse(Response):
selectors from which links cannot be obtained (for instance, anchor tags without an
``href`` attribute)
"""
- arg_count = len(list(filter(None, (urls, css, xpath))))
- if arg_count != 1:
- raise ValueError('Please supply exactly one of the following arguments: urls, css, xpath')
+ arguments = [x for x in (urls, css, xpath) if x is not None]
+ if len(arguments) != 1:
+ raise ValueError(
+ "Please supply exactly one of the following arguments: urls, css, xpath"
+ )
if not urls:
if css:
urls = self.css(css)
diff --git a/tests/test_http_response.py b/tests/test_http_response.py
index be17dfd6b..eafc3560e 100644
--- a/tests/test_http_response.py
+++ b/tests/test_http_response.py
@@ -215,6 +215,10 @@ class BaseResponseTest(unittest.TestCase):
links = map(Link, absolute)
self._assert_followed_all_urls(links, absolute)
+ def test_follow_all_empty(self):
+ r = self.response_class("http://example.com")
+ self.assertEqual([], list(r.follow_all([])))
+
def test_follow_all_invalid(self):
r = self.response_class("http://example.com")
if self.response_class == Response: