Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing bs4 in Python 3.5

I have installed both Python 3.5 and Beautifulsoup4. When I try to import bs4, I get the error below. Is there any fix for that? Or should I just install Python 3.4 instead? Please be very explicit - I am new to programming. Many thanks!

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python 3.5\lib\sit-packages\bs4\__init__.py", line 30, in    <module>
   from .builder import builder_registry, ParserRejectionMarkup
  File "C:\Python 3.5\lib\sit-packages\bs4\__init__.py", line 308, in <module>
   from . import _htmlparser
  File "C:\Python 3.5\lib\sit-packages\bs4\_htmlparser.py", line 7, in <module>
   from html.parser import ( 
ImportError: cannot import name 'HTMLParseError'
like image 717
anne_t Avatar asked Feb 26 '15 14:02

anne_t


People also ask

How do I import a BeautifulSoup in Python 3?

To use beautiful soup, you need to install it: $ pip install beautifulsoup4 . Beautiful Soup also relies on a parser, the default is lxml . You may already have it, but you should check (open IDLE and attempt to import lxml). If not, do: $ pip install lxml or $ apt-get install python-lxml .

Which library is required to import Beautiful Soup?

As BeautifulSoup is not a standard python library, we need to install it first. We are going to install the BeautifulSoup 4 library (also known as BS4), which is the latest one.

What is from BS4 import BeautifulSoup in Python?

Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers hours or days of work.


2 Answers

Update: Starting with 4.4.0, BeautifulSoup is compatible with Python 3.5. Upgrade:

pip install --upgrade beautifulsoup4

Old answer:

Because of the changes made for Deprecate strict mode of HTMLParser issue:

Issue #15114: the strict mode and argument of HTMLParser, HTMLParser.error, and the HTMLParserError exception have been removed.

I'm afraid beautifulSoup4 is not compatible with Python 3.5 at the moment. Use Python 3.4.

like image 62
alecxe Avatar answered Oct 20 '22 04:10

alecxe


Update: BeautifulSoup 4.4.0 has been updated to be python3.5 compatible, so a pip install --upgrade beautifulsoup4 should do the trick if you are still hitting this issue.

like image 24
AnilRedshift Avatar answered Oct 20 '22 04:10

AnilRedshift