简体   繁体   中英

AttributeError: type object '_socketobject' has no attribute 'error'

I am using python 2.7.10 and Phantom JS 1.9.8.

Recently, I started seeing issues on .close().

Specifically:

I set my browser object to `browser = webdriver.PhantomJS()

I then create the BeutifulSoup object:

browser.get(url)
webpage = browser.page_source
soup = BeautifulSoup(webpage, "lxml")

on browser.close(), every now and then I get:

...
except socket.error as s_err:
AttributeError: type object '_socketobject' has no attribute 'error'

Which gets triggered on 'catch clause':

except socket.error as s_err:

I can only assume that the cause for this 'catch' failure is that the socket has actually been closed despite the error and that something else in the close process has triggered the error.

Assuming I am correct, is there a way to actually capture the error?

I have a finally clause that ensures that the browser is terminated (so I do not get a complete crash):

finally:
   browser.quit()

Issues found:

the exception clause except (httplib.HTTPException, socket.error) is wrong:

  • it bundles two unrelated exceptions (not the end of the world but not ok)
  • I actually import errno and from socket import error as socket_error but my exception clause uses a non-existent socket.error (theoretically it should work but...)

I have now split the exceptions and use errno test/check:

except socket_error as s_err:
     if s_err == errno.ECONNREFUSED or s_err == errno.ECONNRESET:
        ...

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