urllib2.urlopen 返回一个没有 info() 或 geturl() 方法的对象
我正在运行 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()
andinfo()
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您的
install_opener
收到了HTTPNtlmAuthHandler
。当该类处理请求时,它返回一个HTTPResponse
而不是标准的“类似文件的对象”。要查看源代码,请访问此处。
The issue is with the fact that you have
install_opener
receive anHTTPNtlmAuthHandler
. When that class handles the request, it returns anHTTPResponse
instead of the standard 'file like object'.To see the source, go here.