17. Python - XML Parsing1
2021. 2. 24. 23:12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | # HTTP 통신 # HTTPCOnnection과 HTTPSConnection 구분 # HTTPConnection("서버주소") # HTTPConnection("www.kma.go.kr") # hc.request("요청방식", "남은주소") 파라메터까지 전부 # hc.request("GET", "/wid/queryDFSRSS.jsp?zone=1132052200") /빼먹지 말기 # 응답 # res = hc.getresponse() # 응답 받은 내용 # resBody = res.read() # 한글 처리해서 출력 # resBody.decode() # XML parsing # kmaWeather = fromstring(resBody) # DOM 객체 여러개 찾기 # .getiterator("태그명") # weathers = kmaWeather.getiterator("data") # DOM 객체 한개 찾기 # .find("태그명") # w.find("hour").text # xml에서 여러개인 DOM 객체인 data에서 하나인 hour 객체 가져오기 hc = HTTPConnection("www.kma.go.kr") hc.request("GET", "/wid/queryDFSRSS.jsp?zone=1132052200") resBody = hc.getresponse().read() # print(resBody.decode()) kmaWeather = fromstring(resBody) weathers = kmaWeather.getiterator("data") for w in weathers: print(w.find("hour").text) print(w.find("temp").text) print(w.find("wfKor").text) print("---------") | cs |
'Python' 카테고리의 다른 글
19. Python - JSON Parsing (0) | 2021.02.25 |
---|---|
18. Python - XML Parsing2 (0) | 2021.02.24 |
16. Python - Exception (0) | 2021.02.24 |
15. Python - 다중상속 (0) | 2021.02.23 |
14. Python - 상속, overriding (0) | 2021.02.23 |