用beautifulsoup4查找具有camelCase标记名的“全部”

2024-05-19 02:29:01 发布

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

我试图用beauthoulsoup4.4.0来获取一个xml文件,该文件在camelCase中有标记名,但是find\u all似乎找不到它们。示例代码:

from bs4 import BeautifulSoup

xml = """
<hello>
    world
</hello>
"""
soup = BeautifulSoup(xml, "lxml")

for x in soup.find_all("hello"):
    print x

xml2 = """
<helloWorld>
    :-)
</helloWorld>
"""
soup = BeautifulSoup(xml2, "lxml")

for x in soup.find_all("helloWorld"):
    print x

我得到的输出是:

^{pr2}$

查找大小写/大写标记名的正确方法是什么?在


Tags: 文件in标记helloforxmlallfind
1条回答
网友
1楼 · 发布于 2024-05-19 02:29:01

对于任何使用beauthoulsoup的区分大小写的解析,您都需要在"xml"模式下进行解析。默认模式(解析HTML)不关心大小写,因为HTML不关心大小写。在您的例子中,不要使用"lxml"模式,而是将其切换到"xml"

from bs4 import BeautifulSoup

xml2 = """
<helloWorld>
    :-)
</helloWorld>
"""
soup = BeautifulSoup(xml2, "xml")

for x in soup.find_all("helloWorld"):
    print x

相关问题 更多 >

    热门问题