【问题标题】:xgoogle library fails with AttributeErrorxgoogle 库因 AttributeError 而失败
【发布时间】:2011-03-08 02:49:42
【问题描述】:

我尝试使用 xgoogle python 库来输入一个术语,它会返回有多少搜索结果。这是我的代码:

from xgoogle.search import GoogleSearch
word1 = 'aardvark'
word2 = 'ablaze'
words = word1,"",word2
gs = GoogleSearch(words)
num = gs.num_results
print num

这正在返回'Traceback(最近一次调用最后一次):

  File "F:\google whack\SearchTest.py", line 6, in <module>
    num = gs.num_results
  File "C:\Python27\xgoogle\search.py", line 89, in num_results
    page = self._get_results_page()
  File "C:\Python27\xgoogle\search.py", line 189, in _get_results_page
    safe_url = [url % { 'query': urllib.quote_plus(self.query),
  File "C:\Python27\lib\urllib.py", line 1245, in quote_plus
    return quote(s, safe)
  File "C:\Python27\lib\urllib.py", line 1236, in quote
    if not s.rstrip(safe):

AttributeError: 'tuple' object has no attribute 'rstrip''

如果有人知道如何使它返回结果的数量,非常感谢帮助!!!谢谢!!!

【问题讨论】:

    标签: python search xgoogle


    【解决方案1】:

    您将 words 作为元组传递。尝试将单词连接在一起:

    gs = GoogleSearch(word1 + " " + word2)
    

    【讨论】:

    • 或 gs = GoogleSearch(' '.join(word1, word2, word3))
    【解决方案2】:

    传递一个字符串是最简单的——不需要创建多个变量。

    gs = GoogleSearch("hello world")
    

    或者,如果你有绑定到多个变量的字符串,你可以加入它们,正如@samplebias 建议的那样,尽管他忘记了 join() 只接受一个参数,通常是一个元组。

    gs = GoogleSearch(' '.join((word1, word2, word3)))
    

    注意多余的一对括号。

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 2019-02-27
      • 2012-06-10
      • 1970-01-01
      相关资源
      最近更新 更多