1
我讀了「美麗的湯4.0.0文檔」 a link 和遇到的問題,進口類NavigableStrign:
我進口一切BS4(BeautifulSoup4.0)下python3:從BS4進口*無法導入下python3
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.
...
然而,無論NavigableString和BeautifulSoup包括在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 *
切線備註:考慮不要使用'import *',永遠。它混淆了像pyflakes這樣的工具,並且讓你的代碼不易讀。 (我不明白他們爲什麼不在Python 3中刪除它。) –