import error due to bs4 vs BeautifulSoup

时光怂恿深爱的人放手 提交于 2019-12-04 14:13:11

问题


I am trying to use beautifulsoup compatible lxml and it is giving me an error:

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

I have bs4 installed. How do I fix this issue?


回答1:


The error is caused by soupparser.py trying to import BeautifulSoup version 3 while you have version 4 installed. The module name was changed from BeautifulSoup to bs4 in version 4.

You can trick soupparser.py into importing version 4 by mapping the bs4 module to BeautifulSoup in sys.modules before importing soupparser:

import sys, bs4
sys.modules['BeautifulSoup'] = bs4

from lxml.html.soupparser import fromstring



回答2:


There is now a version of soupparser that works with bs4. It's available here: https://github.com/lxml/lxml/blob/master/src/lxml/html/soupparser.py




回答3:


Try adding :

from bs4 import BeautifulSoup

and make sure you have the correct version of BeautifulSoup for your system installed.



来源:https://stackoverflow.com/questions/14042023/import-error-due-to-bs4-vs-beautifulsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!