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

2024-10-06 10:32:38 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

我以前从没见过这个。。。在

我在跑步:

^{pr2}$
  • 我已经测试过只放字符串而不是变量,代码运行良好。在
  • 我也测试过使用%s和{}。格式。。。任何帮助将不胜感激!在
  • 最后,我还有一个用户dict.项目而不是在初始化循环时的奇数拉链。。。在

也就是说,当代码编写如下时,我会得到相同的错误:

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

Tags: tokeyinselfforlibpackagessite
1条回答
网友
1楼 · 发布于 2024-10-06 10:32:38

我的一个朋友搞明白了,我不得不明确地加上表示字符串的括号:

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

疯了!在

相关问题 更多 >