# bookstore부터 시작하는 DOM
bookStore = et.Element("bookstore")
book1 = et.Element("book", category="cooking")
bookStore.insert(0, book1)
title1 = et.Element("title")
title1.attrib["lang"] = "en"
title1.text = "Everyday Italian"
book1.append(title1)
# 노드에서 sub element 생성
et.SubElement(book1, "author").text = "Giada De Laurentiis"
et.SubElement(book1, "year").text = "2005"
et.SubElement(book1, "price").text = "30.00"
book2 = et.Element("book", {"category":"children"})
bookStore.append(book2)
title2 = et.Element("title")
title2.attrib["lang"] = title1.get("lang")
title2.text = "Harry Potter"
book2.append(title2)
et.SubElement(book2, "author").text = "Giada De Laurentiis"
et.SubElement(book2, "year").text = "2005"
et.SubElement(book2, "price").text = "30.00"
et.dump(bookStore)
# output
<bookstore><book category="cooking"><title lang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book><book category="children"><title lang="en">Harry Potter</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book></bookstore>
root = et.fromstring(et.tostring(bookStore))
#self.children, list(elem)
childNodes = root.getchildren()
print(len(childNodes))
for childNode in childNodes:
print(childNode.tag, childNode.items())
for childNode in childNodes[0]:
print(childNode.tag, childNode.keys())
if childNode.keys() != []:
print([childNode.get(k) for k in childNode.keys()])
book = root.find("book")
print(book.tag, book.get("category"))
bookList = root.findall("book")
for book in bookList:
print(book.tag, book.get("category"))
# .은 현재위치 - 여기서는 루트이다.
# 즉 루트 아래 자식요소 중에 타이틀인거 찾아라
title = root.find(".//title")
print(type(title), title.text)
titleList = root.findall(".//title")
print([title.text for title in titleList])
title = root.findtext(".//title")
print(type(title), title)
book = root.find(".//book[@category='children']")
print(book, book.tag)
# output
2
book [('category', 'cooking')]
book [('category', 'children')]
title ['lang']
['en']
author []
year []
price []
book cooking
book cooking
book children
<class 'xml.etree.ElementTree.Element'> Everyday Italian
['Everyday Italian', 'Harry Potter']
<class 'str'> Everyday Italian
<Element 'book' at 0x10dd81bd8> book
from lxml import etree
bookStore = etree.Element("bookstore")
book1 = etree.SubElement(bookStore, "book")
book2 = etree.SubElement(bookStore, "book", attrib={"category":"children"})
book1.attrib["category"] = "cooking"
title1 = etree.Element("title", lang="en")
title1.text = "Everyday Italian"
book1.append(title1)
# 노드에서 sub element 생성
etree.SubElement(book1, "author").text = "Giada De Laurentiis"
etree.SubElement(book1, "year").text = "2005"
etree.SubElement(book1, "price").text = "30.00"
title2 = etree.Element("title")
title2.set("lang", title1.get("lang"))
title2.text = "Harry Potter"
book2.append(title2)
etree.SubElement(book2, "author").text = "Giada De Laurentiis"
etree.SubElement(book2, "year").text = "2005"
book2.insert(3, etree.Element("price"))
print(len(book2))
book2[-1].text = "30.00"
xmlBytes = etree.tostring(bookStore, encoding="utf-8", pretty_print=True, xml_declaration=True)
xmlStr = etree.tounicode(bookStore, pretty_print=True)
print(type(xmlBytes), type(xmlStr))
etree.dump(bookStore)
xml = etree.fromstring(etree.tostring(bookStore))
xmlTree = etree.ElementTree(xml)
xmlRoot = xmlTree.getroot()
childNodes = xmlRoot.getchildren()
print(len(childNodes))
for childNode in childNodes:
print(childNode.tag, childNode.items())
for childNode in childNodes[0]:
print(childNode.tag, childNode.keys())
if childNode.keys() != []:
print([childNode.get(k) for k in childNode.keys()])
book = xmlRoot.find("book")
print(book.tag, book.get("category"))
bookList = xmlRoot.findall("book")
for book in bookList:
print(book.tag, book.get("category"))
title = xmlRoot.find(".//title")
print(type(title), title.text)
titleList = xmlRoot.findall(".//title")
print([title.text for title in titleList])
title = xmlRoot.findtext(".//title")
print(type(title), title)
book = xmlRoot.find(".//book[@category='children']")
print(book, book.tag)
for cilldNode in xmlRoot.iter():
print(childNode.tag, childNode.text)
for childNode in xmlRoot.iter("book"):
print(childNode.tag, childNode.text)
import urllib
from lxml import etree
# 서비스 url
url = "http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getMsrstnAcctoRltmMesureDnsty"
# 필요한 파라미터
params = {
"serviceKey" : "개인 키",
"numOfRows" : 10,
"pageSize" : 10,
"pageNo" : 1,
"startPage" : 1,
"stationName" : "노원구",
"dataTerm" : "DAILY",
"ver" : "1.3",
}
params["serviceKey"] = urllib.parse.unquote(params["serviceKey"])
params = urllib.parse.urlencode(params)
params = params.encode("utf-8")
# get 방식으로 url 날림
req = urllib.request.Request(url, data=params)
# request 객체 만들어서 보냄.
res = urllib.request.urlopen(req)
resStr = res.read()
xmlObj = etree.fromstring(resStr)
xmlRoot = etree.ElementTree(xmlObj).getroot()
pm25List = xmlRoot.findall(".//item/pm25Value")
for item in pm25List:
print(item.tag, item.text)
Comments