无法使用 google colab notebook 中 googlesearch 模块的搜索方法

Unable to use search method from googlesearch module in google colab notebook

try: 
    from googlesearch import search 
    
except ImportError:  
    print("No module named 'google' found") 
urls = [] 
for j in search(str(query),tld="com",start=1, num=15, stop=15, pause=0):
        print(j) 
        urls.append(j)

我在本地 Jupyter Notebook 中使用了相同的代码。有用。但是在 google colab notebook 中执行时,它显示了以下错误。

NameError                                 Traceback (most recent call last)
<ipython-input-38-a97ca791b5a9> in <module>()
      4     print("No module named 'google' found")
      5 urls = []
----> 6 for j in google-search(str(query),tld="com",start=1, num=15, stop=15, pause=0):
      7         print(j)
      8         urls.append(j)

NameError: name 'search' is not defined

所有必需的模块都已正确安装。

!pip install rake-nltk
!pip install google-search
!pip install google
!pip install urllib
!pip install beautifulsoup4

答案在这里:https://github.com/MarioVilas/googlesearch/issues/39

该问题与软件包之间的名称冲突有关。如果您只安装 google 而不是 google-search,它会起作用。

!pip install google

如果您已经安装了它们,请使用:

!pip uninstall google-search
!pip uninstall google
!pip install google