[英]Python, LXML ElementTree not making elementtree out of STYLE element
我正在嘗試從 XML 中獲取元素。 似乎對整個文檔工作正常,除非我在 STYLE 元素中點擊任何內容。 然后 lxml 不構建樹。 通過元素后返回正常功能。 我在想它可能是一個保留的元素名稱,但我找不到任何證實這一點的東西。 也許我錯過了一些明顯明顯的東西......
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 & French Ale</CATEGORY>
<CATEGORY_NUMBER>1</CATEGORY_NUMBER>
...
預期結果:
5 style
6 name Witbier
6 version 1
6 category Belgian & French Ale
6 category_number 1
....
使用import lxml.etree
代替import lxml.html
並替換
tree = lxml.html.fromstring(response.content)
和
tree = lxml.etree.fromstring(response.content)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.