我正在尝试使用带有camelCase中的标记名称的BeautifulSoup 4.4.0来抓取xml文件,而find_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
我得到的输出是:
$ python soup_test.py
<hello>
world
</hello>
查找驼峰/大写标签名称的正确方法是什么?
答案 0 :(得分:6)
对于使用BeautifulSoup进行任何区分大小写的解析,您可能希望以"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