3

I am reading the "Beautiful Soup 4.0.0 documentation" a link and encountered an import problem:
I import everything in bs4 (BeautifulSoup4.0) under python3:

from bs4 import *    

but fail to use the class NavigableString :

>>> help(NavigableString)  

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'NavigableString' is not defined

while the class BeautifulSoup is available:

>>> help(BeautifulSoup)

Help on class BeautifulSoup in module bs4:
class BeautifulSoup(bs4.element.Tag)
|  This class defines the basic interface called by the tree builders.
...

However, both NavigableString and BeautifulSoup are included in the namespace of bs4:

>>> import bs4
>>> dir(bs4)  

['BeautifulSoup', 
 'BeautifulStoneSoup', 'CData', 'Comment', 
 'DEFAULT_OUTPUT_ENCODING', 'Declaration', 'Doctype', 
 'NavigableString',
 'PageElement', 'ProcessingInstruction', 'ResultSet', 'SoupStrainer', 
 'StopParsing', 'Tag', 'UnicodeDammit', '__all__', '__author__', 
 '__builtins__', '__cached__', '__copyright__', '__doc__', '__file__', 
 '__license__', '__name__', '__package__', '__path__', '__version__', 
 'builder', 'builder_registry', 'dammit', 'element', 're', 'syntax_error', 
 'warnings']

I know I can use NavigableString by:

from bs4 import NavigableString

I am confused and want to figure out the underlying mechanism.
Does it have something to do with the python package or module hierarchy?
Or the previous import statement?

from bs4 import *
4

1 回答 1

4

bs4.__all__ 包含'BeautifulSoup',因此这是您从导入时*将导入的唯一名称bs4

于 2013-05-17T10:28:49.983 回答