2011-03-08 50 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))) 

請注意額外的一對括號。