0

I am writing a code to open a webpage and interact with it. I am using the mechanize module in pydev. The code so far I have written:

from bs4 import BeautifulSoup
from mechanize import Browser
from mechanize import HTTPError
import re

def main():
    movie='The Incredibles';
    movie_search='+'.join(movie.split());
    base_url= 'http://www.imdb.com/find?q=';
    final_url=base_url+movie_search+'&s=all';
    br=Browser();
    br.open(final_url);
    link=br.find_link(url_regex=re.compile(r'/title/tt.*'));
    dest=br.follow_link(link);
    print(link);

if __name__=="__main__":main()

When compiling I am getting the following error:

Traceback (most recent call last):
  File "D:\python\foldersorter\src\search.py", line 7, in <module>
    from mechanize import Browser
  File "C:\Python34\lib\site-packages\mechanize\__init__.py", line 122, in <module>
    from _mechanize import \
  File "C:\Python34\Lib\site-packages\mechanize\_mechanize.py", line 231
    except urllib2.HTTPError, error:
                            ^
SyntaxError: invalid syntax

What exactly is the syntax error I am unable to find out. I am working in python 3.4. Am I doing something wrong here?

Debdipta Halder
  • 497
  • 1
  • 5
  • 19
  • 1
    the correct python 3 syntax is `except ... as ...` - see [Python try...except comma vs 'as' in except](http://stackoverflow.com/questions/2535760/python-try-except-comma-vs-as-in-except) - looks like the imported library is made for Python 2.x – Aprillion Apr 03 '16 at 17:25

1 Answers1

0

I think you need Python 2 to run mechanize and you are trying to run it with Python 3 (File "C:\Python34\Lib...).

I hope be helpful.

iblancasa
  • 334
  • 1
  • 5
  • Thanks for the assistance. I feel that is the reason. Mechanize is not supported in python 3.0. MechanicalSoup is there for 3.x but its not well documented and does not contains all modules of mechanize. Can you suggest any other browser like mechanize for 3.x – Debdipta Halder Apr 03 '16 at 18:51
  • What do you think about "RoboBrowser"? It's for Python 3.x and I think could be a good alternative – iblancasa Apr 03 '16 at 18:59