我已经用python结合BeautifulSoup编写了一个脚本来解析某些xml
元素中的一些名称,但由于某种原因,该脚本在print语句之前抛出了属性错误。我怎样才能让它工作?提前谢谢
到目前为止,我所尝试的:
from bs4 import BeautifulSoup
content="""
<ns:Car>
<ns:Model>sedan</ns:Model>
<ns:Model>coupe</ns:Model>
<ns:Model>hatchback</ns:Model>
<ns:Model>convertible</ns:Model>
</ns:Car>
"""
soup = BeautifulSoup(content,"xml")
for items in soup.find("ns:Car").find_all("ns:Model"):
print(items)
预期产出:
sedan
coupe
hatchback
convertible
它抛出的错误是:
for items in soup.find("ns:Car").find_all("ns:Model"):
AttributeError: 'NoneType' object has no attribute 'find_all'
顺便说一句,我不愿意遵守任何与regular expression
相关的解决方案。我喜欢使用BeautifulSoup
解析相同的内容
您对
soup.find("ns:Car")
的调用正在返回类型为NoneType
的对象,并且您正在尝试调用此NoneType
对象的find_all
方法。尝试将最后几行更改为:相关问题 更多 >
编程相关推荐