urllib 忽略身份验证请求

发布于 2024-08-24 01:05:33 字数 231 浏览 6 评论 0原文

我在创建使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我为君王 2024-08-31 01:05:33

您对 urllib2.HTTPError 例外:

异常 urllib2.HTTPError

尽管 HTTPError 是一个异常(URLError 的子类),但它也可以用作非异常的类似文件的返回值(与 urlopen() 返回的值相同)。这在处理异常 HTTP 错误时非常有用,例如身份验证请求。

代码

RFC 2616 中定义的 HTTP 状态代码。此数值对应于 BaseHTTPServer.BaseHTTPRequestHandler.responses 中的代码字典中找到的值。

异常的代码属性可用于验证是否需要身份验证 - 代码 401。

>>> try: 
...     conn = urllib2.urlopen('http://www.example.com/admin')
...     # read conn and process data
... except urllib2.HTTPError, x:
...     print 'Ignoring', x.code
...     
Ignoring 401
>>> 

You are right about the urllib2.HTTPError exception:

exception urllib2.HTTPError

Though being an exception (a subclass of URLError), an HTTPError can also function as a non-exceptional file-like return value (the same thing that urlopen() returns). This is useful when handling exotic HTTP errors, such as requests for authentication.

code

An HTTP status code as defined in RFC 2616. This numeric value corresponds to a value found in the dictionary of codes as found in BaseHTTPServer.BaseHTTPRequestHandler.responses.

The code attribute of the exception can be used to verify that authentication is required - code 401.

>>> try: 
...     conn = urllib2.urlopen('http://www.example.com/admin')
...     # read conn and process data
... except urllib2.HTTPError, x:
...     print 'Ignoring', x.code
...     
Ignoring 401
>>> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文