python - <type 'exceptions.AttributeError'>这到底是哪里错了
高洛峰
高洛峰 2017-04-17 13:53:13
[Python讨论组]

python
报错

Exception in thread Thread-1 (most likely raised during interpreter shutdown):
Traceback (most recent call last):
  File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
  File "/usr/lib/python2.6/threading.py", line 484, in run
  File "dxcad.py", line 243, in threadget
  File "dxcad.py", line 181, in gethtml
  File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
  File "/usr/lib/python2.6/urllib2.py", line 391, in open
  File "/usr/lib/python2.6/urllib2.py", line 409, in _open
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
  File "/usr/lib/python2.6/urllib2.py", line 1190, in http_open
  File "/usr/lib/python2.6/urllib2.py", line 1164, in do_open
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'error'

代码

def gethtml(url):
    encoding_support = ContentEncodingProcessor
    prodom=proxylist[random.randint(0,proxn)]
    proxy_support = urllib2.ProxyHandler({'http':prodom})
    opener = urllib2.build_opener(proxy_support,encoding_support,urllib2.HTTPHandler)
    urllib2.install_opener(opener)
    try:
        bpanhtml = urllib2.urlopen(url,timeout = 2).read()
    except Exception,e:
        print e
        return gethtml(url)
    return bpanhtml

求教

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(1)
高洛峰

不要在线程中 install_opener

urllib2.install_opener(opener)
Install an OpenerDirector instance as the default global opener. Installing an opener is only necessary if you want urlopen to use that opener; otherwise, simply call OpenerDirector.open() instead of urlopen(). The code does not check for a real OpenerDirector, and any class with the appropriate interface will work.

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号