如何访问Python http.client.HTTPResponse对象内部?
我试过使用 dir() 这个命令,但它返回的信息是这样的:
<bound method HTTPResponse.begin of <http.client.HTTPResponse object at 0x00E9DEF0>>
我不知道该怎么理解这些信息。
说明一下:我其实对 Python 不是很熟悉,所以这个问题可能听起来很傻。请大家多多包涵。
谢谢!
3 个回答
1
HTTPResponse.read()
这个函数可以用来获取HTTP响应的内容。简单来说,HTTPresponse.read()
会读取一个 http.client.HTTPResponse
对象,并返回响应的主体内容。想了解更多,可以查看Python的官方文档,链接在这里:https://docs.python.org/3/library/http.client.html#httpresponse-objects
response = urllib.request.urlopen(some_request)
body = response.read()
注意:urllib.request.urlopen(some_request)
会返回一个HTTP响应对象
1
你可以像这样逐行打印一个HTTPResponse对象的内容
# Get the object from a url
url = 'https://www.example.com'
response_object = urllib.request.urlopen(url)
# Print the contents line by line
for line in response_object:
print(line)
3
方法和函数一样,在调用的时候必须加上括号(()
),括号里可以选择性地放入参数。
someobj.somemeth()