3

「Beautiful Soup 4.0.0 ドキュメント」 のリンク を読んでいて、インポートの問題が発生しました。python3 の下でbs4 (BeautifulSoup4.0) の
すべてをインポートします。

from bs4 import *    

ただし、クラスNavigableStringの使用に失敗します:

>>> help(NavigableString)  

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

クラスBeautifulSoupが利用可能である間:

>>> 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.
...

ただし、NavigableStringBeautifulSoupの両方が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']

次の方法でNavigableStringを使用できることを知っています。

from bs4 import NavigableString

私は混乱しており、根本的なメカニズムを理解したいと思っています。
Python パッケージまたはモジュール階層と関係がありますか?
または以前のインポートステートメントですか?

from bs4 import *
4

1 に答える 1

4

bs4.__all__ のみが含まれ'BeautifulSoup'ているため、からインポートするときにインポート*される唯一の名前ですbs4

于 2013-05-17T10:28:49.983 に答える