将变量传递给 :contains() 选择器、pyquery/cssselect、错误

Posted

技术标签:

【中文标题】将变量传递给 :contains() 选择器、pyquery/cssselect、错误【英文标题】:Passing variable to :contains() selector, pyquery/cssselect, error 【发布时间】:2016-03-04 05:43:21 【问题描述】:

我正在使用 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>]

【问题讨论】:

不是答案,但请将zip(user_type_regexes.keys(), user_type_regexes.values())替换为user_type_regexes.items() 【参考方案1】:

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

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

疯了!

【讨论】:

以上是关于将变量传递给 :contains() 选择器、pyquery/cssselect、错误的主要内容,如果未能解决你的问题,请参考以下文章

如何将 settings.py 中的变量传递给视图?

我可以在 Web2Py 中包含部分视图,将特定变量传递给它吗?

如何将局部变量传递给熊猫中的用户定义函数?

将函数变量传递给标签

将绑定传递给类型属性包装器的变量 - 失去基础类型

将实例变量传递给装饰器