diff --git a/docs/usage.rst b/docs/usage.rst index f5950a88..5e32cf77 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -1130,7 +1130,7 @@ Converting CSS to XPath When you're using an API that only accepts XPath expressions, it's sometimes useful to convert CSS to XPath. This allows you to take advantage of the -conciseness of CSS to query elements by classes and the easeness of +conciseness of CSS to query elements by classes and the easiness of manipulating XPath expressions at the same time. On those occasions, use the function :func:`~parsel.css2xpath`: diff --git a/parsel/selector.py b/parsel/selector.py index 504a4feb..fff04268 100644 --- a/parsel/selector.py +++ b/parsel/selector.py @@ -3,6 +3,7 @@ """ import sys +from warnings import warn import six from lxml import etree, html @@ -73,7 +74,7 @@ def __getitem__(self, pos): def __getstate__(self): raise TypeError("can't pickle SelectorList objects") - def xpath(self, xpath, namespaces=None, **kwargs): + def xpath(self, query=None, namespaces=None, **kwargs): """ Call the ``.xpath()`` method for each element in this list and return their results flattened as another :class:`SelectorList`. @@ -90,7 +91,18 @@ def xpath(self, xpath, namespaces=None, **kwargs): selector.xpath('//a[href=$url]', url="http://www.example.com") """ - return self.__class__(flatten([x.xpath(xpath, namespaces=namespaces, **kwargs) for x in self])) + if query is None: + query = kwargs.pop("xpath", None) + if query is not None: + warn( + "The parameter 'xpath' is deprecated, use 'query' instead.", + DeprecationWarning, + stacklevel=2 + ) + else: + raise ValueError("The 'query' parameter is missing or None") + + return self.__class__(flatten([x.xpath(query, namespaces=namespaces, **kwargs) for x in self])) def css(self, query): """