簡體   English   中英

Beautifulsoup:ImportError:沒有名為 html.entities 的模塊

[英]beautifulsoup: ImportError: No module named html.entities

我試圖讓這個模塊在服務器上工作,但標題中出現錯誤:

我的腳本:

from bs4 import BeautifulSoup

當我運行它時:

aclark@tycho ~ % python test.py
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from bs4 import BeautifulSoup
  File "/usr/lib/python2.7/site-packages/bs4/__init__.py", line 30, in <module>
    from .builder import builder_registry, ParserRejectedMarkup
  File "/usr/lib/python2.7/site-packages/bs4/builder/__init__.py", line 4, in <module>
    from bs4.element import (
  File "/usr/lib/python2.7/site-packages/bs4/element.py", line 5, in <module>
    from bs4.dammit import EntitySubstitution
  File "/usr/lib/python2.7/site-packages/bs4/dammit.py", line 11, in <module>
    from html.entities import codepoint2name
ImportError: No module named html.entities

現在,我看到了ImportError: No module named html.entities

這將我重定向到http://www.crummy.com/software/BeautifulSoup/bs4/doc/#problems-after-installation

基於此:

aclark@tycho ~ % sudo pip uninstall BeautifulSoup
Uninstalling BeautifulSoup:
  /usr/lib/python2.7/site-packages/BeautifulSoup-3.2.1-py2.7.egg-info
  /usr/lib/python2.7/site-packages/BeautifulSoup.py
  /usr/lib/python2.7/site-packages/BeautifulSoup.pyc
  /usr/lib/python2.7/site-packages/BeautifulSoupTests.py
  /usr/lib/python2.7/site-packages/BeautifulSoupTests.pyc
Proceed (y/n)? y
  Successfully uninstalled BeautifulSoup`

`aclark@tycho ~ % sudo pip install BeautifulSoup
Downloading/unpacking BeautifulSoup
  Running setup.py egg_info for package BeautifulSoup

Installing collected packages: BeautifulSoup
  Running setup.py install for BeautifulSoup

Successfully installed BeautifulSoup
Cleaning up...

行為無變化:

aclark@tycho ~ % python test.py 
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from bs4 import BeautifulSoup
  File "/usr/lib/python2.7/site-packages/bs4/__init__.py", line 30, in <module>
    from .builder import builder_registry, ParserRejectedMarkup
  File "/usr/lib/python2.7/site-packages/bs4/builder/__init__.py", line 4, in <module>
    from bs4.element import (
  File "/usr/lib/python2.7/site-packages/bs4/element.py", line 5, in <module>
    from bs4.dammit import EntitySubstitution
  File "/usr/lib/python2.7/site-packages/bs4/dammit.py", line 11, in <module>
    from html.entities import codepoint2name
ImportError: No module named html.entities

我什至從服務器中刪除了 python3 修復它,但同樣的問題。

誰能指出我更好的方向,卸載,重新安裝?

如何確保安裝了 2.7 版的代碼?

干杯

亞當

我也遇到了這個問題。

最后,我發現這只是因為我在腳本文件的同一目錄中有一個名為“html.py”的文件。

因此,當 BeautifulSoup "from html.entities import codepoint2name" 時,它拋出異常......

我在不同的情況下遇到了類似的問題。

pip install future

應該做的伎倆。 查看http://python-future.org/standard_library_imports.html了解更多信息。

我在嘗試導入 gtts 時遇到了同樣的問題(我使用的是 python3)

from gtts import gTTS

通過在命令行上安裝 html-entities 解決了這個問題: $ npm install html-entities

然后在腳本中from html.entities import codepoint2name

不確定這是否是一個好方法,但它有效

我在使用 pytest 對包含 html 的命名空間包( mycorp.whatever.html )使用 pytest 時遇到了問題,源代碼和 test 位於同一目錄中。

即這個:

├── mycorp
│   ├── whatever
│   │   ├── html
│   │   ├── tests

Pytest 在測試發現期間更改 python 路徑,如果測試與您的代碼混合,它會將html命名空間添加到您的路徑中,請參見此處

pytest 會找到 foo/bar/tests/test_foo.py 並意識到它是一個包的一部分,因為在同一個文件夾中有一個init .py 文件。 然后它將向上搜索,直到找到最后一個仍包含init .py 文件的文件夾,以便找到包根目錄(在本例中為 foo/)。 為了加載模塊,它會在 sys.path 的前面插入 root/(如果還沒有),以便加載 test_foo.py 作為模塊 foo.bar.tests.test_foo。

因此,為了解決此問題,請更改您的架構,以便測試和源不在同一目錄中。

即這個:

├── tests
├── mycorp
│   ├── whatever
│   │   ├── html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM