简体   繁体   English

如何使用BeautifulSoup解决此命名空间冲突?

[英]How to solve this namespace conflict with BeautifulSoup?

I'm having some trouble with Python namespaces. 我在使用Python名称空间时遇到了麻烦。

With import bs4 import bs4

>>> import bs4
>>> print type(x)
>>> <class 'bs4.element.NavigableString'>
>>> isinstance(x,bs4.element.NavigableString)
>>> True

But I want to use from bs4 import BeautifulSoup and then I get an error 但我想使用from bs4 import BeautifulSoup ,然后出现错误

>>> from bs4 import BeautifulSoup
>>> print type(x)
>>> <class 'bs4.element.NavigableString'>
>>> isinstance(x,bs4.element.NavigableString)
>>> ...
NameError: name 'bs4' is not defined


isinstance(x,bs4.element.NavigableString)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
             What should go here? 

Checked also isinstance not working correctly with beautifulsoup(NameError) but it didn't help me. 检查了isinstance与beautifulsoup(NameError)不能正常工作,但没有帮助我。

正如@jonrsharpe所说,用from bs4 import BeautifulSoup, element替换import语句from bs4 import BeautifulSoup, element并用element.NavigableString替换有问题的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM