urllib 忽略身份验证请求
我在创建使用 URL 的脚本时遇到了一些问题。我正在使用 urllib.urlopen() 来获取所需 URL 的内容。但其中一些 URL 需要身份验证。 urlopen 提示我输入用户名,然后输入密码。 我需要的是忽略每个需要身份验证的 URL,只需轻松跳过它并继续,有没有办法做到这一点? 我想知道如何捕获 HTTPError 异常,但实际上,异常是由 urlopen() 方法处理的,所以它不起作用。
感谢您的每一个回复。
I'm having little trouble creating a script working with URLs. I'm using urllib.urlopen() to get content of desired URL. But some of these URLs requires authentication. And urlopen prompts me to type in my username and then password.
What I need is to ignore every URL that'll require authentication, just easily skip it and continue, is there a way to do this?
I was wondering about catching HTTPError exception, but in fact, exception is handled by urlopen() method, so it's not working.
Thanks for every reply.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对
urllib2.HTTPError
例外:异常的代码属性可用于验证是否需要身份验证 - 代码 401。
You are right about the
urllib2.HTTPError
exception:The code attribute of the exception can be used to verify that authentication is required - code 401.