2011-03-08 46 views
0

我尝试使用xgoogle python函数库来输入一个术语,并且它返回了多少个搜索结果。这里是我的代码:xgoogle函数库因AttributeError失败

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

这回“回溯(最近通话最后一个):

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

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

回答

1

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

gs = GoogleSearch(word1 + " " + word2) 
+0

或gs = GoogleSearch(''.join(word1,word2,word3)) – 2011-03-08 03:14:17

1

传递字符串最简单 - 不需要创建多个变量。

gs = GoogleSearch("hello world") 

或者,如果您有绑定到多个变量的字符串,就可以加入他们的行列,为@samplebias暗示,尽管他忘了join()方法只需要一个参数,通常是一个元组。

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

请注意额外的一对括号。