Beautiful Soup – Porting Old Code


Beautiful Soup – Porting Old Code



”;


You can make the code from earlier version of Beautiful Soup compatible with the lates version by making following change in the import statement −

Example


from BeautifulSoup import BeautifulSoup
#becomes this:

from bs4 import BeautifulSoup

If you get the ImportError “No module named BeautifulSoup”, it means you”re trying to run Beautiful Soup 3 code, but you only have Beautiful Soup 4 installed. Similarly, If you get the ImportError “No module named bs4″, because you”re trying to run Beautiful Soup 4 code, but you only have Beautiful Soup 3 installed.

Beautiful Soup 3 used Python”s SGMLParser, a module that has been removed in Python 3.0. Beautiful Soup 4 uses html.parser by default, but you can also use lxml or html5lib.

Although BS4 is mostly backwards-compatible with BS3, most of its methods have been deprecated and given new names for PEP 8 compliance.

Here are a few examples −


replaceWith -> replace_with
findAll -> find_all
findNext -> find_next
findParent -> find_parent
findParents -> find_parents
findPrevious -> find_previous
getText -> get_text
nextSibling -> next_sibling
previousSibling -> previous_sibling

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *