Why does beautiful soup not properly parse element named "area"?

Viewed 128

I'm writing a python script that uses beautiful soup to parse xml documents. Some of the documents contain elements named "area". For some reason I cannot for the life of me properly parse these elements. They always come out as empty <area/> elements.

Here is a minimal example of what is happening:

#!/usr/bin/python3.5
from bs4 import BeautifulSoup

xml = """""
<?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?>

<root>
    <areax>
        foo
    </areax>
    <area>
        bar
    </area>
</root>
"""""
soup = BeautifulSoup (xml, "lxml")

print ("\n#### soup ####\n")
print (soup)

print ("\n#### areax ####\n")
areaxs = soup.find_all ("areax")
for areax in areaxs:
    print (areax)

print ("\n### area ###\n")
areas = soup.find_all ("area")
for area in areas:
    print (area)

Output:

#### soup ####

<html><body><p>""
<?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?>
<root>
<areax>
        foo
    </areax>
<area/>
        bar

</root>
</p></body></html>

#### areax ####

<areax>
        foo
    </areax>

### area ###

<area/>

Is the element name "area" protected in any way or is there something else wrong with how I'm parsing it?

1 Answers

Your document is parsed as HTML, and the area element is an empty HTML element (cannot have any children).

To parse it as XML instead, use BeautifulSoup(xml, "xml") (docs):

By default, Beautiful Soup parses documents as HTML. To parse a document as XML, pass in “xml” as the second argument to the BeautifulSoup constructor:

soup = BeautifulSoup(markup, "xml")

You’ll need to have lxml installed.


The other problem is that you have too many quotes surrounding your xml string, so it actually starts with "" (try printing it). Exactly three quotes (""") should suffice.

Related