Python - Beautiful Soup 4 - 'NavigableString' 对象没有属性 'find_all'

Python - Beautiful Soup 4 - 'NavigableString' object has no attribute 'find_all'

我有以下代码:

for route in parsedXml.find_all('route'):
    print(dataCheck(route.find('routesectionref')))
    for routeSection in parsedXml.find(id = dataCheck(route.find('routesectionref'))):
        for routeLink in routeSection.find_all('routelink'):
            print(routeLink)

哪个 return 错误:

Exception has occurred: AttributeError
'NavigableString' object has no attribute 'find_all'

问题是“路线”是“bs4.element.Tag”。 “routeSection”是一个“bs4.element.NavigableString”。如何将我的“routeSection”变量声明修改为 return a “bs4.element.Tag”?

'NavigableString' object has no attribute 'find_all' 可能会发生,因为 BeautifulSoup 解析的实际上是一个字符串(或包含一个字符串),而不是 HTML / XML 标记。

Ref: "AttributeError: 'NavigableString' object has no attribute 'foo' - 这通常是因为您将字符串视为标签。您可能正在遍历列表,期望它只包含标签, 当它实际上包含标签和字符串时。"