Python 由于bs4 vs ULSOUP而导致导入错误
我试图使用Python 由于bs4 vs ULSOUP而导致导入错误,python,beautifulsoup,lxml,bs4,Python,Beautifulsoup,Lxml,Bs4,我试图使用beautifulsoupcompatiblelxml,但它给了我一个错误: from lxml.html.soupparser import fromstring Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Python/2.7/site-packages/lxml/html/soupparser.py", line 7, i
beautifulsoup
compatiblelxml
,但它给了我一个错误:
from lxml.html.soupparser import fromstring
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/lxml/html/soupparser.py", line 7, in <module>
from BeautifulSoup import \
ImportError: No module named BeautifulSoup
从lxml.html.soupparser导入fromstring
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/Library/Python/2.7/site packages/lxml/html/soupparser.py”,第7行,在
从美联进口\
ImportError:没有名为BeautifulSoup的模块
我已经安装了bs4。如何解决此问题?尝试添加:
来自bs4导入美化组
并确保您安装了适用于您的系统的正确版本的
BeautifulSoup
。此错误是由于在安装版本4的同时尝试导入BeautifulSoupParser.py导致的。在版本4中,模块名称已从BeautifulSoup
更改为bs4
在导入soupparser
之前,通过将bs4
模块映射到中的BeautifulSoup
,可以将soupparser.py
诱骗到导入版本4中:
import sys, bs4
sys.modules['BeautifulSoup'] = bs4
from lxml.html.soupparser import fromstring
现在有一个版本的soupparser与bs4一起工作。此处提供:查看此信息,我已安装beautifulsoup版本4,因此从bs4导入beautifulsoup效果良好,但lxml.html.soupparser使用beautifulsoup 3,并具有beautifulsoup导入xyz的语句。soupparser与bs4配合使用是否良好?如果是,为什么它仍然使用旧版本?我们是否应该将soupparser更改为尝试将bs4作为BeautifulSoup导入,然后仅在失败时导入BeautifulSoup?假设soupparser.py可以在lxml的Github repo中进行修改,那么为未来的用户进行更改并消除这个问题应该很简单,对吧?soupparser现在已经移植到bs4。请参阅单独的答案。