
正文
Python查看对象属性的方法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
帮助https://docs.python.org/2/library/functions.html
dir() 函数
D:\pythontest>python
Python 3.5. (v3.5.0:374f501f4567, Sep , ::) [MSC v. bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> res = requests.get('https://www.sogou.com/link?url=DSOYnZeCC_popvGkrcLjd__A2
nuyEZQRtN0U1Xf4Goe7gAE91qpuJw..')
>>>
>>>
>>> dir(res)
['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__
doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribu
te__', '__getstate__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '
__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__redu
ce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '_
_subclasshook__', '__weakref__', '_content', '_content_consumed', '_next', 'appa
rent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encodin
g', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content'
, 'iter_lines', 'json', 'links', 'next', 'ok', 'raise_for_status', 'raw', 'reaso
n', 'request', 'status_code', 'text', 'url']
>>>
>>>
>>> print(res.url)
https://www.sogou.com/link?url=DSOYnZeCC_popvGkrcLjd__A2nuyEZQRtN0U1Xf4Goe7gAE91
qpuJw..
>>> print(res.text)
<meta content="always" name="referrer"><script>window.location.replace("http://w
ww.hujiang.com/ciku/register/")</script><noscript><META http-equiv="refresh" con
tent="0;URL='http://www.hujiang.com/ciku/register/'"></noscript>
help() 函数
调用内置帮助系统
>>> help(res)
Help on Response in module requests.models object: class Response(builtins.object)
| The :class:`Response <Response>` object, which contains a
| server's response to an HTTP request.
|
| Methods defined here:
|
| __bool__(self)
| Returns True if :attr:`status_code` is less than .
|
| This attribute checks if the status code of the response is between
| and to see if there was a client error or a server error. If
| the status code, is between and , this will return True. This
| is **not** a check to see if the response code is `` OK``.
|
| __enter__(self)
|
| __exit__(self, *args)
|
| __getstate__(self)
|
| __init__(self)
| Initialize self. See help(type(self)) for accurate signature.
|
|
|
|
|
|
|
|
| -- More -- #按q退出
vars() 函数
vars ( [ object ] ) 返回object对象的__dict__属性,其中object对象可以是模块,类,实例,或任何其他有__dict__属性的对象。所以,其与直接访问__dict__属性等价。示例如下(这里是反例,mser对象中没有__dict__属性):
>>> vars(res)
{'request': <PreparedRequest [GET]>, 'reason': 'OK', 'headers': {'Server': 'ngin
x', 'UUID': '6e3340cd-af97-40b4-bc43-f61aef2f27fc', 'Content-Type': 'text/html;
charset=GBK', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding', 'Expires':
'Sat, 24 Aug 2019 16:01:02 GMT', 'Content-Encoding': 'gzip', 'Date': 'Sat, 24 Au
g :: GMT', 'Set-Cookie': 'ABTEST=||v17; expires=Mon, -Se
p- :: GMT; path=/, IPLOC=CN4601; expires=Sun, -Aug- :: GMT; do
main=.sogou.com; path=/, SUID=E5FF1D6F2513910A000000005D615F3E; expires=Fri, -
Aug- :: GMT; domain=.sogou.com; path=/, black_passportid=; domain=.so
gou.com; path=/; expires=Thu, -Dec- :: GMT', 'Transfer-Encoding': 'c
hunked', 'P3P': 'CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE CO
M NAV OTC NOI DSP COR", CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA
PRE COM NAV OTC NOI DSP COR", CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT
DEM STA PRE COM NAV OTC NOI DSP COR"', 'Cache-Control': 'max-age=0'}, 'status_co
de': 200, '_content_consumed': True, 'history': [], 'elapsed': datetime.timedelt
a(, , ), '_content': b'<meta content="always" name="referrer"><script>wi
ndow.location.replace("http://www.hujiang.com/ciku/register/")</script><noscript
><META http-equiv="refresh" content="0;URL=\'http://www.hujiang.com/ciku/registe
r/\'"></noscript>', 'cookies': <RequestsCookieJar[Cookie(version=, name='IPLOC'
, value='CN4601', port=None, port_specified=False, domain='.sogou.com', domain_s
pecified=True, domain_initial_dot=True, path='/', path_specified=True, secure=Fa
lse, expires=, discard=False, comment=None, comment_url=None, rest={},
rfc2109=False), Cookie(version=, name='SUID', value='E5FF1D6F2513910A000000005
D615F3E', port=None, port_specified=False, domain='.sogou.com', domain_specified
=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, exp
ires=, discard=False, comment=None, comment_url=None, rest={}, rfc2109
=False), Cookie(version=, name='ABTEST', value='0|1566662462|v17', port=None, p
ort_specified=False, domain='www.sogou.com', domain_specified=False, domain_init
ial_dot=False, path='/', path_specified=True, secure=False, expires=,
discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>, 'url':
'https://www.sogou.com/link?url=DSOYnZeCC_popvGkrcLjd__A2nuyEZQRtN0U1Xf4Goe7gAE
91qpuJw..', '_next': None, 'encoding': 'GBK', 'raw': <urllib3.response.HTTPRespo
nse object at 0x00000000034BE9B0>, 'connection': <requests.adapters.HTTPAdapter
object at 0x00000000034AA4A8>}
>>>
type() 函数
type ( object )返回对象object的类型。
>>> type(res)
<class 'requests.models.Response'>
callable() 函数
callable ( object ): 若object对象是可调用的,则返回True,否则返回False。注意,即使返回True也可能调用失败,但返回False调用一定失败。
>>> callable(res)
False







