列出对象的所有属性

2024-09-27 17:53:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试导入和使用一个名为“维基百科”的模块在这里找到。。。在

https://github.com/goldsmith/Wikipedia

我可以使用dir函数检查所有属性。在

>>> dir(wikipedia)
['BeautifulSoup', 'DisambiguationError', 'PageError', 'RedirectError', 'WikipediaPage', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'cache', 'donate', 'exceptions', 'page', 'random', 'requests', 'search', 'suggest', 'summary', 'util', 'wikipedia']

但是维基百科.page不返回其所有子属性(!?)在

^{pr2}$

我希望在这个列表中看到像标题,内容这样的属性。我如何知道“页面”中隐藏的属性是什么?在


Tags: 模块函数httpsgithubcom属性dirpage
2条回答

你可能还想看看__dict__这是一个不错的小邓德

>>> class foo(object):
...     def __init__(self,thing):
...         self.thing= thing
...
>>> a = foo('pi')
>>> a.__dict__
{'thing': 'pi'}

或者vars也可以做同样的事情:

^{pr2}$

因为wikipedia.page是一个函数。我想您需要的是WikipediaPage对象的属性。在

>>> import wikipedia
>>> ny = wikipedia.page('New York')
>>> dir(ny)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'content', 'html', 'images', 'links', 'load', 'original_title', 'pageid', 'references', 'summary', 'title', 'url']

这两种类型是不同的。在

^{pr2}$

相关问题 更多 >

    热门问题