为什么靓汤不能正确解析名为“area”的元素?

2024-06-16 11:12:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个python脚本,它使用beautiful soup来解析xml文档。有些文档包含名为“area”的元素。出于某种原因,我一生都无法正确解析这些元素。它们总是作为空的<area/>元素出现。你知道吗

下面是一个最简单的例子:

#!/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)

输出:

#### 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/>

元素名“area”是否受到保护,或者我解析它的方式是否有其他问题?你知道吗


Tags: 文档元素fooversionarearootxmlutf
1条回答
网友
1楼 · 发布于 2024-06-16 11:12:07

文档被解析为HTML,^{}元素是空的HTML元素(不能有任何子元素)。你知道吗

要将其解析为XML,请使用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.


另一个问题是xml字符串周围有太多引号,因此它实际上是以""开头的(请尝试打印它)。正好三个引号(""")就足够了。你知道吗

相关问题 更多 >