1. 程式人生 > >xml 讀取

xml 讀取

ElementTree


from xml.etree import ElementTree as ET

# xml_file = r"D:\Deep_Learning_data\yolo\gen_data\test_xml\2007_000323.xml"
root = ET.parse(xml_file).getroot()
img_name = root.find("filename").text

lxml

from lxml import etree

html = etree.HTML(open('web.html',encoding='utf-8').read())  # 容錯率高
'''
    選取節點
'''
# print(len(html.xpath('//div')))
# print(len(html.xpath('/html/body/div')))     # 從根節點開始查詢
# print(len(html.xpath('//div/a')))            # 從全文中開始查詢
# print(len(html.xpath('//div/a/..')))         # 查詢該節點的父節點 . 該節點自己
# print(html.xpath("//div[@class='left']/a"))  # [@class='xxx']  查詢屬性