Why I get urllib2.HTTPError with urllib2 and no errors with urllib?

陌路散爱 提交于 2019-12-03 14:54:43

The original urllib simply does not raise an exception on a 403 code. If you add print c.getcode() to the last line of your program, urllib will reach it and still print out 403.

Then if you do print c.read() at the end, you will see that you did indeed get an error page from Wikipedia. It's just a matter of urllib2 deciding to treat an error 403 as a runtime exception, versus urllib allowing you to still get an error 403 and then do something with the page.

Wikipedia seems to be filtering out urllib2's default User-Agent. Just change it.

kcome

Overriding urllib2.HTTPError or urllib.error.HTTPError and reading response HTML anyway this post shows some nice way to obtain detailed error message from server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!