我试着导入和使用一个叫做“维基百科”的模块。
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']但是wikipedia.page不返回它的所有子属性(!?)
>>> dir(wikipedia.page)
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']我希望在这个列表中看到像标题、内容这样的属性。我如何知道隐藏在“页面”中的属性是什么?
发布于 2013-09-03 05:22:52
因为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']这两种类型是不同的。
>>> type(ny)
<class 'wikipedia.wikipedia.WikipediaPage'>
>>> type(wikipedia.page)
<type 'function'>发布于 2013-09-03 17:42:28
你可能还想看看__dict__,它是一个很好的lil。
>>> class foo(object):
... def __init__(self,thing):
... self.thing= thing
...
>>> a = foo('pi')
>>> a.__dict__
{'thing': 'pi'}或者做同样事情的vars:
>>> vars(a)
{'thing': 'pi'}https://stackoverflow.com/questions/18584263
复制相似问题