本文介绍了导入错误由于BS4 VS BeautifulSoup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 beautifulsoup 兼容 LXML ,这是给我的错误:

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

BS4 安装。我该如何解决这个问题?

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

推荐答案

该错误是由 soupparser.py 试图引起的,而你有版本4安装导入BeautifulSoup 3版。该模块的名字从 BeautifulSoup 4版改为 BS4

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.

您可以欺骗 soupparser.py 到由 BS4 模块映射到 BeautifulSoup sys.modules中 soupparser C $ C> 导入前:

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

这篇关于导入错误由于BS4 VS BeautifulSoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 07:20