Skip to content

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

Remove CSS*Selector classes and port its tests

Daniel Graña681af6b2581b
diff --git a/scrapy/selector/__init__.py b/scrapy/selector/__init__.py
index a1203f987..bfbde4de9 100644
--- a/scrapy/selector/__init__.py
+++ b/scrapy/selector/__init__.py
@@ -3,4 +3,3 @@ Selectors
"""
from scrapy.selector.unified import *
from scrapy.selector.lxmlsel import *
-from scrapy.selector.csssel import *
diff --git a/scrapy/selector/csssel.py b/scrapy/selector/csssel.py
deleted file mode 100644
index a2fc12e8d..000000000
--- a/scrapy/selector/csssel.py
+++ /dev/null
@@ -1,16 +0,0 @@
-from .unified import Selector
-
-
-class CSSSelector(Selector):
-
- default_contenttype = 'html'
-
- def select(self, css):
- return self.css(css)
-
-
-HtmlCSSSelector = CSSSelector
-
-
-class XmlCSSSelector(CSSSelector):
- default_contenttype = 'xml'
diff --git a/scrapy/selector/unified.py b/scrapy/selector/unified.py
index 12d0188ab..77cd9e11f 100644
--- a/scrapy/selector/unified.py
+++ b/scrapy/selector/unified.py
@@ -50,7 +50,7 @@ class Selector(object_ref):
def __init__(self, response=None, text=None, namespaces=None, contenttype=None,
_root=None, _expr=None):
- self.contenttype = ct = self.default_contenttype or _ct(contenttype)
+ self.contenttype = ct = _ct(response, contenttype or self.default_contenttype)
self._parser = _ctgroup[ct]['_parser']
self._csstranslator = _ctgroup[ct]['_csstranslator']
self._tostring_method = _ctgroup[ct]['_tostring_method']
@@ -146,8 +146,11 @@ class SelectorList(list):
def __getslice__(self, i, j):
return self.__class__(list.__getslice__(self, i, j))
- def select(self, xpath):
- return self.__class__(flatten([x.select(xpath) for x in self]))
+ def xpath(self, xpath):
+ return self.__class__(flatten([x.xpath(xpath) for x in self]))
+
+ def css(self, xpath):
+ return self.__class__(flatten([x.css(xpath) for x in self]))
def re(self, regex):
return flatten([x.re(regex) for x in self])
@@ -155,10 +158,15 @@ class SelectorList(list):
def extract(self):
return [x.extract() for x in self]
- @deprecated(use_instead='SelectorList.extract')
+ @deprecated(use_instead='.extract()')
def extract_unquoted(self):
return [x.extract_unquoted() for x in self]
- @deprecated(use_instead='SelectorList.select')
+ @deprecated(use_instead='.xpath()')
def x(self, xpath):
return self.select(xpath)
+
+ @deprecated(use_instead='.xpath()')
+ def select(self, xpath):
+ return self.xpath(xpath)
+
diff --git a/scrapy/tests/test_selector_cssselect.py b/scrapy/tests/test_selector_cssselect.py
index 399820240..dd2e292b6 100644
--- a/scrapy/tests/test_selector_cssselect.py
+++ b/scrapy/tests/test_selector_cssselect.py
@@ -2,9 +2,9 @@
Selector tests for cssselect backend
"""
from twisted.trial import unittest
-from scrapy.http import TextResponse, HtmlResponse, XmlResponse
-from scrapy.selector import CSSSelector, XmlCSSSelector, HtmlCSSSelector
+from scrapy.http import HtmlResponse
from scrapy.selector.csstranslator import ScrapyHTMLTranslator
+from scrapy.selector import Selector
from cssselect.parser import SelectorSyntaxError
from cssselect.xpath import ExpressionError
@@ -116,20 +116,20 @@ class TranslatorMixinTest(unittest.TestCase):
class HTMLCSSSelectorTest(unittest.TestCase):
- hcs_cls = HtmlCSSSelector
+ hcs_cls = Selector
def setUp(self):
self.htmlresponse = HtmlResponse('http://example.com', body=HTMLBODY)
self.hcs = self.hcs_cls(self.htmlresponse)
def x(self, *a, **kw):
- return [v.strip() for v in self.hcs.select(*a, **kw).extract() if v.strip()]
+ return [v.strip() for v in self.hcs.css(*a, **kw).extract() if v.strip()]
def test_selector_simple(self):
- for x in self.hcs.select('input'):
+ for x in self.hcs.css('input'):
self.assertTrue(isinstance(x, self.hcs.__class__), x)
- self.assertEqual(self.hcs.select('input').extract(),
- [x.extract() for x in self.hcs.select('input')])
+ self.assertEqual(self.hcs.css('input').extract(),
+ [x.extract() for x in self.hcs.css('input')])
def test_text_pseudo_element(self):
self.assertEqual(self.x('#p-b2'), [u'<b id="p-b2">guy</b>'])
@@ -147,7 +147,7 @@ class HTMLCSSSelectorTest(unittest.TestCase):
self.assertEqual(self.x('map[name="dummymap"] ::attr(shape)'), [u'circle', u'default'])
def test_nested_selector(self):
- self.assertEqual(self.hcs.select('p').select('b::text').extract(),
+ self.assertEqual(self.hcs.css('p').css('b::text').extract(),
[u'hi', u'guy'])
- self.assertEqual(self.hcs.select('div').select('area:last-child').extract(),
+ self.assertEqual(self.hcs.css('div').css('area:last-child').extract(),
[u'<area shape="default" id="area-nohref">'])