Tag Archives: from bs4 import BeautifulSoup

from bs4 import BeautifulSoup Error [How to Solve]

Importing BeautifulSoup, the following errors (of two kinds) occur.

First error.

Python 2.7.14 (default, Sep 17 2017, 18:50:44)
[GCC 7.2.0] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
File “/usr/local/lib/python2.7/dist-packages/bs4/__init__.py”, line 30, in <module>
from .builder import builder_registry, ParserRejectedMarkup
File “/usr/local/lib/python2.7/dist-packages/bs4/builder/__init__.py”, line 311, in <module>
from . import _html5lib
File “/usr/local/lib/python2.7/dist-packages/bs4/builder/_html5lib.py”, line 57, in <module>
class TreeBuilderForHtml5lib(html5lib.treebuilders._base.TreeBuilder):
AttributeError: ‘module’ object has no attribute ‘_base’
>>>

Solution: As the blogger said, change the name from base.py to _base.py and from base.pyc to _base.pyc.

The paths are generally the same, just copy them.

Error 2:

Then found: the file name is wrong, can not be bs4, so changed the file name, found that still reported an error: the file name is not correct.

But found a runtime cache file did not change over, and then delete, fixed

How to Solve Beautifulsoup Error: “No module named ‘bs4’.” [from bs4 import BeautifulSoup]

When we install beautiful soup 4 in PIP, we will report an error when we import beautiful soup 4: no module named’BS4′

  pip install beautifulsoup4

Then, we write this in the python script:

  from bs4 import BeautifulSoup4

The above error “no module named’BS4′” is reported during execution

After reading some methods on the Internet, I finally solved the problem in this way (on the premise that I used pychar)

Step 1:

Step 2:

Step 3: 

Step 4:

Step 5:

Step 6: If you execute the python program again, no error will be reported