urllib2.urlopen 返回一个没有 info() 或 geturl() 方法的对象

发布于 2024-11-17 02:35:54 字数 1453 浏览 2 评论 0原文

我正在运行 Python 2.7.1,并尝试使用 urllib2 模块访问一些网页。根据 Python 文档,urllib2.urlopen()

该函数返回一个类似文件的对象,带有两个附加方法, geturl()info()

但是,当我尝试在最后一个 print 语句中访问 info() 时,它表明没有这样的方法在代码中。我收到以下错误:

AttributeError: HTTPResponse instance has no attribute 'info'

我不明白这一点。我在 Google 上找不到任何内容,文档清楚地表明这些方法与返回的对象一起存在。有趣的是,正如错误和方法测试所示,它使用这些相应的方法返回一个 HTTPresponse 对象。我缺少什么?

我的代码如下:

import urllib2 
import httplib, socket 
import cookielib
import ntlm 
from ntlm import ntlm 

url = URLOFSOMESORT
user = USERNAMEHERE
password = PASSWORD 
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
data = ""
headers = { 'User-Agent' : user_agent }

passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 
passman.add_password(None, url, user, password) 

cookie_jar = cookielib.CookieJar() 
cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
redirect = urllib2.HTTPRedirectHandler()
auth_basic = urllib2.HTTPBasicAuthHandler(passman) 
auth_digest = urllib2.HTTPDigestAuthHandler(passman) 
auth_NTLM = HTTPNtlmAuthHandler(passman) 

opener = urllib2.build_opener(cookie_handler, auth_NTLM, auth_basic, auth_digest, redirect)
urllib2.install_opener(opener) 
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request) 
#cookie_jar.extract_cookies(response, request)

print response.info()

I am running Python 2.7.1 and am trying to use the urllib2 module to access some webpages. Per the Python documentation, urllib2.urlopen():

This function returns a file-like object with two additional methods,
geturl() and info()

However, it indicates there are no such methods when I try to access info() in the last print statement in the code. I get the following error:

AttributeError: HTTPResponse instance has no attribute 'info'

I don't understand this. I can't find anything on Google and the documentation clearly says that these methods exist with the object returned. Interestingly enough, as indicated through the error and by testing for the methods, it IS returning an HTTPresponse object with those respective methods. What am I missing?

My code is as follows:

import urllib2 
import httplib, socket 
import cookielib
import ntlm 
from ntlm import ntlm 

url = URLOFSOMESORT
user = USERNAMEHERE
password = PASSWORD 
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
data = ""
headers = { 'User-Agent' : user_agent }

passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 
passman.add_password(None, url, user, password) 

cookie_jar = cookielib.CookieJar() 
cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
redirect = urllib2.HTTPRedirectHandler()
auth_basic = urllib2.HTTPBasicAuthHandler(passman) 
auth_digest = urllib2.HTTPDigestAuthHandler(passman) 
auth_NTLM = HTTPNtlmAuthHandler(passman) 

opener = urllib2.build_opener(cookie_handler, auth_NTLM, auth_basic, auth_digest, redirect)
urllib2.install_opener(opener) 
request = urllib2.Request(url, data, headers)
response = urllib2.urlopen(request) 
#cookie_jar.extract_cookies(response, request)

print response.info()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

半世晨晓 2024-11-24 02:35:54

问题在于您的 install_opener 收到了 HTTPNtlmAuthHandler。当该类处理请求时,它返回一个 HTTPResponse 而不是标准的“类似文件的对象”。

要查看源代码,请访问此处

The issue is with the fact that you have install_opener receive an HTTPNtlmAuthHandler. When that class handles the request, it returns an HTTPResponse instead of the standard 'file like object'.

To see the source, go here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文