开发者

xgoogle library fails with AttributeError

开发者 https://www.devze.com 2023-02-15 05:24 出处:网络
I am try to use the xgoogle python library for the purpose of me entering a term, and it returning how many search results there are for it. Here is开发者_开发百科 my code:

I am try to use the xgoogle python library for the purpose of me entering a term, and it returning how many search results there are for it. Here is开发者_开发百科 my code:

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

This is returning 'Traceback (most recent call last):

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

If anybody knows how to make this return the number of results, help is very much appreciated!!! Thanks!!!


You're passing words as a tuple. Try to concat the words together instead:

gs = GoogleSearch(word1 + " " + word2)


Passing a string is simplest -- there is no need to create multiple variables.

gs = GoogleSearch("hello world")

Or, if you have strings bound to multiple variables, you can join them, as @samplebias suggests, although he forgot that join() only takes a single parameter, usually a tuple.

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

Note the extra pair of parentheses.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号