Skip to content

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

Selector should not receive both response and text

Νικόλαος-Διγενής Καραγιάννης643dbeffcf69
diff --git a/scrapy/selector/unified.py b/scrapy/selector/unified.py
index 15f3d26df..64cb0232c 100644
--- a/scrapy/selector/unified.py
+++ b/scrapy/selector/unified.py
@@ -46,6 +46,10 @@ class Selector(_ParselSelector, object_ref):
selectorlist_cls = SelectorList
def __init__(self, response=None, text=None, type=None, root=None, _root=None, **kwargs):
+ if not(response is None or text is None):
+ raise ValueError('%s.__init__() received both response and text'
+ % self.__class__.__name__)
+
st = _st(response, type or self._default_type)
if _root is not None:
diff --git a/tests/test_selector.py b/tests/test_selector.py
index 141455b66..af0cc4de2 100644
--- a/tests/test_selector.py
+++ b/tests/test_selector.py
@@ -123,6 +123,10 @@ class SelectorTestCase(unittest.TestCase):
sel.xpath('//p').extract_unquoted()
self.assertSubstring('Use .extract() instead', str(w[-1].message))
+ def test_selector_bad_args(self):
+ with self.assertRaisesRegexp(ValueError, 'received both response and text'):
+ Selector(TextResponse(url='http://example.com', body=b''), text=u'')
+
class DeprecatedXpathSelectorTest(unittest.TestCase):