[英]Python, LXML ElementTree not making elementtree out of STYLE element
I'm trying to get elements from an XML.我正在尝试从 XML 中获取元素。 Seems to be working fine for the entire document except when I hit anything in the STYLE element.
似乎对整个文档工作正常,除非我在 STYLE 元素中点击任何内容。 Then lxml doesn't build the tree.
然后 lxml 不构建树。 Returns to normal function once past the element.
通过元素后返回正常功能。 I was thinking it might have been a reserved element name, but I cannot find anything confirming this.
我在想它可能是一个保留的元素名称,但我找不到任何证实这一点的东西。 Maybe I'm missing something blatantly obvious...
也许我错过了一些明显明显的东西......
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)
Result:结果:
5 style
<NAME>Witbier</NAME>
<VERSION>1</VERSION>
<CATEGORY>Belgian & French Ale</CATEGORY>
<CATEGORY_NUMBER>1</CATEGORY_NUMBER>
...
Expected result:预期结果:
5 style
6 name Witbier
6 version 1
6 category Belgian & French Ale
6 category_number 1
....
Use import lxml.etree
instead import lxml.html
使用
import lxml.etree
代替import lxml.html
And replace并替换
tree = lxml.html.fromstring(response.content)
with和
tree = lxml.etree.fromstring(response.content)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.