简体   繁体   中英

How to get around urllib2.HTTP when running a python script

I'm trying to run a python script in Windows 7 in the command prompt, however I keep receiving this error every time I try to run it.

File "inb4404.py", line 73
  except urllib2.HTTPError, err:
                          ^
Syntax Error: invalid syntax

I'm not sure what I'm doing wrong. I located the directory containing the python script using 'cd' and also have Python in my PATH. Then I wrote 'python inb4404.py' and received that error.

Any pointers would be greatly appreciated, thank you.

Your syntax is wrong. See https://docs.python.org/3/tutorial/errors.html . If you want err to be the error object, it should be except urllib2.HTTPError as err:

The script you are using is written for Python 2 but you are running it using Python 3.

Depending on what your goals are, you have a few options. The simplest would be to run the script using Python 2. You may already have it in the c:\\Python27\\python directory or can install it. See these Window tips for details or download it here .

If you want to get more involved, you can manually change the script over to Python 3. This would take some reading up on the differences such as the one you encountered with the except urllib2.HTTPError, err becoming except urllib2.HTTPError as err . The script you are running is small so it may be doable but if it relies on Python 2 only libraries it could get more complex fast. Additional tools and info for converting Python 2 to 3 is available at 2to3 which provides a tool that identifies issues and can even automatically fix them for you but like most automatic tools, it may not do everything you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM