mirror of https://github.com/scrapy/scrapy.git
upgrade parsel and add shim for deprecated selectorlist methods
This commit is contained in:
parent
e50610bd3a
commit
766c255152
|
|
@ -7,4 +7,4 @@ queuelib
|
|||
six>=1.5.2
|
||||
PyDispatcher>=2.0.5
|
||||
service_identity
|
||||
parsel>=0.9.3
|
||||
parsel>=0.9.5
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ XPath selectors based on lxml
|
|||
"""
|
||||
|
||||
import warnings
|
||||
from parsel import Selector as ParselSelector, SelectorList
|
||||
from parsel import Selector as ParselSelector, SelectorList as ParselSelectorList
|
||||
from scrapy.utils.trackref import object_ref
|
||||
from scrapy.utils.python import to_bytes
|
||||
from scrapy.http import HtmlResponse, XmlResponse
|
||||
|
|
@ -26,9 +26,24 @@ def _response_from_text(text, st):
|
|||
body=to_bytes(text, 'utf-8'))
|
||||
|
||||
|
||||
class SelectorList(ParselSelectorList, object_ref):
|
||||
@deprecated(use_instead='.extract()')
|
||||
def extract_unquoted(self):
|
||||
return [x.extract_unquoted() for x in self]
|
||||
|
||||
@deprecated(use_instead='.xpath()')
|
||||
def x(self, xpath):
|
||||
return self.select(xpath)
|
||||
|
||||
@deprecated(use_instead='.xpath()')
|
||||
def select(self, xpath):
|
||||
return self.xpath(xpath)
|
||||
|
||||
|
||||
class Selector(ParselSelector, object_ref):
|
||||
|
||||
__slots__ = ['response']
|
||||
selectorlist_cls = SelectorList
|
||||
|
||||
def __init__(self, response=None, text=None, type=None, root=None, _root=None, **kwargs):
|
||||
st = _st(response, type or self._default_type)
|
||||
|
|
|
|||
|
|
@ -101,6 +101,28 @@ class SelectorTestCase(unittest.TestCase):
|
|||
assert not hasattr(x, '__dict__'), "%s does not use __slots__" % \
|
||||
x.__class__.__name__
|
||||
|
||||
def test_deprecated_selector_methods(self):
|
||||
sel = Selector(TextResponse(url="http://example.com", body=b'<p>some text</p>'))
|
||||
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
sel.select('//p')
|
||||
self.assertSubstring('Use .xpath() instead', str(w[-1].message))
|
||||
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
sel.extract_unquoted()
|
||||
self.assertSubstring('Use .extract() instead', str(w[-1].message))
|
||||
|
||||
def test_deprecated_selectorlist_methods(self):
|
||||
sel = Selector(TextResponse(url="http://example.com", body=b'<p>some text</p>'))
|
||||
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
sel.xpath('//p').select('.')
|
||||
self.assertSubstring('Use .xpath() instead', str(w[-1].message))
|
||||
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
sel.xpath('//p').extract_unquoted()
|
||||
self.assertSubstring('Use .extract() instead', str(w[-1].message))
|
||||
|
||||
|
||||
class DeprecatedXpathSelectorTest(unittest.TestCase):
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue