Search code examples
python-3.5urllib2google-search

google-search urllib2 error


iam using python 3.5 and google-search module but when run below it gives me error

from googlesearch.googlesearch import GoogleSearch

response = GoogleSearch().search("anything")
for result in response.results:
    print("Title: " + result.title)
    print("Content: " + result.getText())

ERROR:

Traceback (most recent call last):
  File "C:/Users/xxx/Document/xxx/google_search.py", line 1, in <module>
   from googlesearch.googlesearch import GoogleSearch
  File "C:\Python35\lib\site-packages\googlesearch\googlesearch.py", line 6, in <module>
    import urllib2
ImportError: No module named 'urllib2

'


Solution

  • urllib2 is a Python 2 library. You should use urllib.request library if you want to stay with Python 3. They're practically the same.

    Edit: seems like the googlesearch package is for Python 2. You can use a virtualenv to build up a py2 environment. To successfully use the py3 print() function, you should add this line at the beginning of your script:

    from __future__ import print_function