728x90
반응형
xml etree를 이용하여 rss 에서 원하는 정보를 추출하는 예제이다.
기상청에서 제공하는 날씨 데이터 rss를 첨부 파일에 저장했다.
---
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
반응형
'프로그래밍응용 > Python' 카테고리의 다른 글
[scraping/crwaling] csv 및 json 형식으로 저장. (0) | 2020.11.27 |
---|---|
[scraping/crwaling] url library 이용한 간단한 페이지 코드 수집 (0) | 2020.11.27 |
Ubuntu 20.04 에서 Python3, 가상 환경(python-venv) 설치하기 (1) | 2020.11.27 |