728x90
반응형

xml etree를 이용하여 rss 에서 원하는 정보를 추출하는 예제이다.

기상청에서 제공하는 날씨 데이터 rss를 첨부 파일에 저장했다.

rss.xml
0.09MB

---

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from xml.etree import ElementTree # XML Parser
 
# parse 함수로 읽고 분석 후, ElementTree 객체 생성
tree = ElementTree.parse('./rss.xml')
# 해당 트리의 root 요소 반환
root = tree.getroot()
# 해당 경로에 해당하는 요소들의 갯수를 출력
print(len(root.findall('channel/item/description/body/location')))
# 추출
for loc in root.findall('channel/item/description/body/location'):
    print('지역 [{}] 의 기상청 소식입니다 -------------------'.format(loc.find('city').text))
    for data in loc.findall('data'):
        tm_ef = data.find('tmEf').text
        tmn = data.find('tmn').text
        tmx = data.find('tmx').text
        wf = data.find('wf').text
 
        print(tm_ef, tmn, tmx, wf)
cs

위 코드에 대한 결과이다.

728x90
반응형

+ Recent posts