Python,LXML ElementTree 没有从 STYLE 元素中生成元素树

Python, LXML ElementTree not making elementtree out of STYLE element

我正在尝试从 XML 中获取元素。似乎整个文档都可以正常工作,除非我点击 STYLE 元素中的任何内容。然后 lxml 不构建树。 Returns 一旦通过元素即可正常运行。我在想它可能是一个保留的元素名称,但我找不到任何可以证实这一点的东西。也许我遗漏了一些明显的东西...

import requests
import lxml.html

response = requests.get('http://www.beerxml.com/recipes.xml')

def depth(node):
    d = 0
    while node is not None:
        d += 1
        node = node.getparent()
    return d

tree = lxml.html.fromstring(response.content)

for recipe in tree:
  for child in recipe.iter():
    print(depth(child),child.tag, '\t\t\t',child.text)

结果:

5 style              
 <NAME>Witbier</NAME>
 <VERSION>1</VERSION>
 <CATEGORY>Belgian &amp; French Ale</CATEGORY>
 <CATEGORY_NUMBER>1</CATEGORY_NUMBER>
...

预期结果:

5 style              
6 name Witbier
6 version 1
6 category Belgian &amp; French Ale
6 category_number 1
....

使用import lxml.etree代替import lxml.html

并替换

tree = lxml.html.fromstring(response.content)

tree = lxml.etree.fromstring(response.content)