1

I'm using pyquery to scrape some data and would like to iterate over some key words combined with matching regular expressions.

I try passing the keys as variables, however I keep getting the following error:

ExpressionError: Expected a single string for :contains(), got [<IDENT 'Trad' at 10>]

I've never seen this before...

I am running:

user_type_regexes = {'Trad': rock_re, "Sport": rock_re, 
                 "Boulders": boulder_re, "Aid": aid_re, 
                 "Ice": ice_re, "Mixed": mixed_re}

user_diffs = user_main('table')

for key, value in zip(user_type_regexes.keys(), user_type_regexes.values()):
    if key != "Boulders":
        tdwithkey = user_diffs.find("tr").children(':contains(' + key + ')') 
  • I have tested just putting strings instead of the variable and the code works fine.
  • I have also tested using %s and {}.format... Any help would be greatly appreciated!
  • Lastly, I have also user dict.items instead of the odd zip while initializing the loop...

i.e. I get the same exact error when the code is written as below:

for key, value in user_type_regexes.items():
    if key != "Boulders":
        tdwithkey = user_diffs.find("tr").children(':contains({})'.format(key)) 

and this:

for key, value in user_type_regexes.items():
    if key != "Boulders":
        tdwithkey = user_diffs.find("tr").children(':contains(%s)' % key) 

The full error report is:

ExpressionError                           Traceback (most recent call last)
<ipython-input-18-e159185e499d> in <module>()
 11 for key, value in zip(user_type_regexes.keys(), user_type_regexes.values()):
 12     if key != "Boulders":
---> 13         tdwithkey = user_diffs.find("tr").children(':contains(' + key + ')')
 14         leadhtml = tdwithkey.next().html()
 15         followhtml = tdwithkey.next().next().html()

C:\Users\nolefp\Anaconda\lib\site-packages\pyquery\pyquery.pyc in children(self, selector)
532         """
533         elements = [child for tag in self for child in tag.getchildren()]
--> 534         return self._filter_only(selector, elements)
535 
536     def closest(self, selector=None):

C:\Users\nolefp\Anaconda\lib\site-packages\pyquery\pyquery.pyc in _filter_only(self, selector, elements, reverse, unique)
413             results = elements
414         else:
--> 415             xpath = self._css_to_xpath(selector, 'self::')
416             results = []
417             for tag in elements:

C:\Users\nolefp\Anaconda\lib\site-packages\pyquery\pyquery.pyc in _css_to_xpath(self, selector, prefix)
247     def _css_to_xpath(self, selector, prefix='descendant-or-self::'):
248         selector = selector.replace('[@', '[')
--> 249         return self._translator.css_to_xpath(selector, prefix)
250 
251     def __call__(self, *args, **kwargs):

C:\Users\nolefp\Anaconda\lib\site-packages\cssselect\xpath.pyc in css_to_xpath(self, css, prefix)
190         return ' | '.join(self.selector_to_xpath(selector, prefix,
191                                                  translate_pseudo_elements=True)
--> 192                           for selector in parse(css))
193 
194     def selector_to_xpath(self, selector, prefix='descendant-or-self::',

C:\Users\nolefp\Anaconda\lib\site-packages\cssselect\xpath.pyc in <genexpr>((selector,))
190         return ' | '.join(self.selector_to_xpath(selector, prefix,
191                                                  translate_pseudo_elements=True)
--> 192                           for selector in parse(css))
193 
194     def selector_to_xpath(self, selector, prefix='descendant-or-self::',

C:\Users\nolefp\Anaconda\lib\site-packages\cssselect\xpath.pyc in selector_to_xpath(self, selector, prefix, translate_pseudo_elements)
217         if not tree:
218             raise TypeError('Expected a parsed selector, got %r' % (selector,))
--> 219         xpath = self.xpath(tree)
220         assert isinstance(xpath, self.xpathexpr_cls)  # help debug a missing 'return'
221         if translate_pseudo_elements and selector.pseudo_element:

C:\Users\nolefp\Anaconda\lib\site-packages\cssselect\xpath.pyc in xpath(self, parsed_selector)
252         if method is None:
253             raise ExpressionError('%s is not supported.' %  type_name)
--> 254         return method(parsed_selector)
255 
256 

C:\Users\nolefp\Anaconda\lib\site-packages\cssselect\xpath.pyc in xpath_function(self, function)
280             raise ExpressionError(
281                 "The pseudo-class :%s() is unknown" % function.name)
--> 282         return method(self.xpath(function.selector), function)
283 
284     def xpath_pseudo(self, pseudo):

C:\Users\nolefp\Anaconda\lib\site-packages\pyquery\cssselectpatch.pyc in xpath_contains_function(self, xpath, function)
415             raise ExpressionError(
416                 "Expected a single string for :contains(), got %r" % (
--> 417                     function.arguments,))
418 
419         value = self.xpath_literal(function.arguments[0].value)

ExpressionError: Expected a single string for :contains(), got [<IDENT 'Trad' at 10>]
  • 1
    not an answer, but please replace `zip(user_type_regexes.keys(), user_type_regexes.values())` with `user_type_regexes.items()` – roippi Nov 30 '15 at 14:56

1 Answers1

1

A friend of mine figured it out, I had to explicitly add parentheses indicating a string:

"td:contains('{}')".format(key)

crazy!