1

我正在使用 pyquery 来抓取一些数据,并希望迭代一些结合匹配正则表达式的关键词。

我尝试将键作为变量传递,但是我不断收到以下错误:

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

我以前从未见过这个...

我在跑步:

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 + ')') 
  • 我已经测试过只放置字符串而不是变量,并且代码工作正常。
  • 我还使用 %s 和 {}.format 进行了测试...任何帮助将不胜感激!
  • 最后,在初始化循环时,我还有用户 dict.items 而不是奇怪的 zip ......

即当代码编写如下时,我得到了同样的错误:

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

和这个:

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

完整的错误报告是:

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>]
4

1 回答 1

0

我的一个朋友想通了,我必须明确地添加括号来表示一个字符串:

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

疯狂的!

于 2015-11-30T17:05:58.253 回答