首页 > 解决方案 > ImportError:无法从“bs4.element”导入名称“样式表”

问题描述

我想在访问网站后获得一些链接。导入 BeautifulSoup 时出现以下错误。

Traceback (most recent call last):
  File "e:/User/Python/CF.py", line 3, in <module>
    from bs4 import BeautifulSoup
  File "C:\Users\CP\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\__init__.py", line 29, in <module>
    from .builder import builder_registry
  File "C:\Users\CP\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\builder\__init__.py", line 7, in <module>
    from bs4.element import (
ImportError: cannot import name 'Stylesheet' from 'bs4.element' (C:\Users\CP\AppData\Local\Programs\Python\Python38\lib\site-packages\bs4\element.py)

这是我安装它的方法。 pip install beautifulsoup4然后 cd 到目录并运行Python setup.py install

我也试过重新安装它。

代码没什么大不了的。当我删除它时它工作正常from bs4 import BeautifulSoup

import requests 
from bs4 import BeautifulSoup

URL = "https://www.facebook.com/"
r = requests.get(URL) 

我还尝试将文件名从 Report.py 更改为 CF.py

标签: pythonweb-scrapingbeautifulsoup

解决方案


我认为python 3.8有问题。我对 Python3.8.6 有同样的问题。因此,您可以更改解释器或卸载 Python3.8 并安装另一个版本。


推荐阅读