我正在編寫一個代碼來打開網頁並與之交互。我在pydev中使用機械化模塊。代碼到目前爲止,我已經寫了:爲什麼我在使用機械化時遇到HTTPerror
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()
當編譯我收到以下錯誤:
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
到底是什麼語法錯誤,我無法找到答案。我在Python 3.4中工作。我在這裏做錯了什麼?
的。正確的Python 3語法是'except ... as ...' - 請參閱[Python嘗試...除了逗號vs'as'除外](http://stackoverflow.com/questions/2535760/python-try-except -comma-vs-as-in-except) - 看起來像導入的庫是爲Python 2.x編寫的 – Aprillion