Python:urllib2.HTTPError:HTTP错误300:多选

Python:urllib2.HTTPError:HTTP错误300:多选,python,web,urllib2,http-error,Python,Web,Urllib2,Http Error,我有一个脚本,它在web文本页中查找信息,然后将它们存储在字典中。 脚本正在寻找一个列表中的URL,然后在一个循环中对它们进行处理,但是在这个过程中间会被这个错误打断: Traceback (most recent call last): File "<stdin>", line 3, in <module> File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.

我有一个脚本,它在web文本页中查找信息,然后将它们存储在字典中。 脚本正在寻找一个列表中的URL,然后在一个循环中对它们进行处理,但是在这个过程中间会被这个错误打断:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 406, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 444, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 300: Multiple Choices

web服务器正在返回HTTP状态代码300多选(请参阅),用于您要访问的URL之一。这可能意味着列表中的一个URL错误,web服务器希望通过提供类似现有URL的列表来帮助您

一般来说,
urllib2
会将任何不成功或简单重定向响应的内容转换为异常,这就是您在那里看到的

当您不在某处处理异常时,例如使用
try except
块,它通常会终止您的程序。因此,您需要将对
urlopen
的调用包装在一个try块中:

try:
  textfile = urllib2.urlopen(item);
except urllib2.HTTPError:
  # Do something here to handle the error. For example:
  print("URL", item, "could not be read.")
  continue
try:
  textfile = urllib2.urlopen(item);
except urllib2.HTTPError:
  # Do something here to handle the error. For example:
  print("URL", item, "could not be read.")
  continue