这些是什么错误,我该如何处理? [英] What are these errors and how do I handle them?

查看:265
本文介绍了这些是什么错误,我该如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个简单的code

 对于L在BIOS:
    OpenThisLink = URL + 1
    响应= urllib2.urlopen(OpenThisLink)

开约200 URL和与正则表达式(和BeautifulSoup)寻找他们,但一打左右后,我得到这些错误和IDLE退出。他们的意思是什么?我该如何处理?

感谢您。

 回溯(最后最近一次调用):  文件\\ PROJECTS \\ JD \\ jd10.py,15号线,上述<&模块GT;响应= urllib2.urlopen(OpenThisLink)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,线路124,在_opener.open的urlopen回报(网址,数据,超时)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,389线,在公开回应=甲基(REQ,响应)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,线路502,在HTTP_RESPONSE的http,请求,响应,code,味精,HDRS)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,线421,在错误结果= self._call_chain(*参数)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,361线,在_call_chain结果= FUNC(*参数)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,线路597,在返回的http_error_302 self.parent.open(新)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,389线,在公开回应=甲基(REQ,响应)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,线路502,在HTTP_RESPONSE的http,请求,响应,code,味精,HDRS)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,线421,在错误结果= self._call_chain(*参数)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,361线,在_call_chain结果= FUNC(*参数)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,线路597,在返回的http_error_302 self.parent.open(新)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,389线,在公开回应=甲基(REQ,响应)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,线路502,在HTTP_RESPONSE的http,请求,响应,code,味精,HDRS)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,线路427,在错误返回self._call_chain(*参数)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,361线,在_call_chain结果= FUNC(*参数)  文件C:\\ Python26 \\ lib目录\\ urllib2.py,线510,在http_error_default引发HTTPError(req.get_full_url(),code,味精,HDRS,FP)HTTPError这样的:HTTP错误404:未找到


  

    

      
    
  

解决方案

被提出的误差 HTTPError这样 - 具体而言,404被抛出你的网址之一。你既可以忽略它:

 对于L在BIOS:
    OpenThisLink = URL + 1
    尝试:
        响应= urllib2.urlopen(OpenThisLink)
    除了urllib2.HTTPError:
        通过

或者,你可以用(略)更有意义的消息重新引发错误:

 对于L在BIOS:
    OpenThisLink = URL + 1
    尝试:
        响应= urllib2.urlopen(OpenThisLink)
    除了urllib2.HTTPError为e:
        引发异常('错误打开%s:%s'的%(e.geturl(),e)条)

I am using this simple code

for l in bios:
    OpenThisLink = url + l
    response = urllib2.urlopen(OpenThisLink)

to open about 200 urls and search them with regex (and BeautifulSoup), but after a dozen or so I get these errors and IDLE quits. What do they mean? How can I handle them?

Thank you.

Traceback (most recent call last):

  File "\PROJECTS\JD\jd10.py", line 15, in <module> response = urllib2.urlopen(OpenThisLink)

  File "C:\Python26\lib\urllib2.py", line 124, in urlopen return _opener.open(url, data, timeout)

  File "C:\Python26\lib\urllib2.py", line 389, in open response = meth(req, response)

  File "C:\Python26\lib\urllib2.py", line 502, in http_response 'http', request, response, code, msg, hdrs)

  File "C:\Python26\lib\urllib2.py", line 421, in error result = self._call_chain(*args)

  File "C:\Python26\lib\urllib2.py", line 361, in _call_chain result = func(*args)

  File "C:\Python26\lib\urllib2.py", line 597, in http_error_302 return self.parent.open(new)

  File "C:\Python26\lib\urllib2.py", line 389, in open response = meth(req, response)

  File "C:\Python26\lib\urllib2.py", line 502, in http_response 'http', request, response, code, msg, hdrs)

  File "C:\Python26\lib\urllib2.py", line 421, in error result = self._call_chain(*args)

  File "C:\Python26\lib\urllib2.py", line 361, in _call_chain result = func(*args)

  File "C:\Python26\lib\urllib2.py", line 597, in http_error_302 return self.parent.open(new)

  File "C:\Python26\lib\urllib2.py", line 389, in open response = meth(req, response)

  File "C:\Python26\lib\urllib2.py", line 502, in http_response 'http', request, response, code, msg, hdrs)

  File "C:\Python26\lib\urllib2.py", line 427, in error return self._call_chain(*args)

  File "C:\Python26\lib\urllib2.py", line 361, in _call_chain result = func(*args)

  File "C:\Python26\lib\urllib2.py", line 510, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 404: Not Found

解决方案

The error being raised is HTTPError - specifically, a 404 is being thrown for one of your URLs. You could either ignore it:

for l in bios:
    OpenThisLink = url + l
    try:
        response = urllib2.urlopen(OpenThisLink)
    except urllib2.HTTPError:
        pass

Or, you could re-raise the error with a (marginally) more meaningful message:

for l in bios:
    OpenThisLink = url + l
    try:
        response = urllib2.urlopen(OpenThisLink)
    except urllib2.HTTPError as e:
        raise Exception('Error opening %s: %s' % (e.geturl(), e))

这篇关于这些是什么错误,我该如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆