from lxml import etree
from xml.etree import cElementTree as ET
tree = ET.parse(r"D:\General\Python_Preference_validation\sample.xml")
root = tree.getroot()
root[0][1].text
這裏獲取元素的值是XML文件: <?xml
我使用Python lxml的客戶,我嘗試下面的代碼解析,並得到了我想要的元素,但它只是返回空: from lxml import html
tree = html.fromstring(html_content)
posts = tree.xpath('//*[@id="posts"]/div')
for post in posts:
print post
的HTML代碼如下