gawel / pyquery

A jquery-like library for python
http://pyquery.rtfd.org/
Other
2.3k stars 182 forks source link

Problem with :first object has no attribute 'add_post_condition' #91

Closed korjavin closed 5 years ago

korjavin commented 9 years ago
from pyquery import PyQuery as pq
d = pq(url='http://whatthecommit.com/')
print d("div#content > p:first ").text()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/pyquery/pyquery.py", line 237, in __call__
    result = self.__class__(*args, parent=self, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pyquery/pyquery.py", line 213, in __init__
    xpath = self._css_to_xpath(selector)
  File "/usr/lib/python2.7/dist-packages/pyquery/pyquery.py", line 223, in _css_to_xpath
    return self._translator.css_to_xpath(selector, prefix)
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 192, in css_to_xpath
    for selector in parse(css))
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 192, in <genexpr>
    for selector in parse(css))
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 219, in selector_to_xpath
    xpath = self.xpath(tree)
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 254, in xpath
    return method(parsed_selector)
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 264, in xpath_combinedselector
    self.xpath(combined.subselector))
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 254, in xpath
    return method(parsed_selector)
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 292, in xpath_pseudo
    return method(self.xpath(pseudo.selector))
  File "/usr/lib/python2.7/dist-packages/pyquery/cssselectpatch.py", line 19, in xpath_first_pseudo
    xpath.add_post_condition('position() = 1')
AttributeError: 'XPathExpr' object has no attribute 'add_post_condition'
d("div")("p:first")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/pyquery/pyquery.py", line 237, in __call__
    result = self.__class__(*args, parent=self, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pyquery/pyquery.py", line 213, in __init__
    xpath = self._css_to_xpath(selector)
  File "/usr/lib/python2.7/dist-packages/pyquery/pyquery.py", line 223, in _css_to_xpath
    return self._translator.css_to_xpath(selector, prefix)
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 192, in css_to_xpath
    for selector in parse(css))
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 192, in <genexpr>
    for selector in parse(css))
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 219, in selector_to_xpath
    xpath = self.xpath(tree)
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 254, in xpath
    return method(parsed_selector)
  File "/usr/lib/python2.7/dist-packages/cssselect/xpath.py", line 292, in xpath_pseudo
    return method(self.xpath(pseudo.selector))
  File "/usr/lib/python2.7/dist-packages/pyquery/cssselectpatch.py", line 19, in xpath_first_pseudo
    xpath.add_post_condition('position() = 1')
AttributeError: 'XPathExpr' object has no attribute 'add_post_condition'
 apt-cache show python-pyquery
Package: python-pyquery
Source: pyquery
Version: 1.2.4-1
gawel commented 9 years ago

Works for me

In [1]: from pyquery import PyQuery as pq

In [2]: d = pq(url='http://whatthecommit.com/')

In [3]: print d("div#content > p:first ").text()
Something fixed

Which cssselect version are you using ? You may use a too old/recent version

korjavin commented 9 years ago
apt-cache show python-cssselect                                                                                                         
Package: python-cssselect
Version: 0.9.1+git90c72b0-1
gawel commented 9 years ago

I guess it's fixed in pyquery's latest version. At least master. Because I still cant reproduce with cssselect 0.9.1

Can you try with the master ?