2025-09-10:仓库迁移
This commit is contained in:
17
6.数据编码与处理/3.解析简单的XML文档.py
Normal file
17
6.数据编码与处理/3.解析简单的XML文档.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from urllib.request import urlopen
|
||||
from xml.etree.ElementTree import parse
|
||||
|
||||
if __name__ == "__main__":
|
||||
u = urlopen('http://planet.python.org/rss20.xml')
|
||||
# 使用xml.etree.ElementTree的parse方法来解析文档
|
||||
doc = parse(u)
|
||||
print(doc)
|
||||
|
||||
# 可以使用.iterfind来对解析出来的文档进行迭代,找到所有指定名称的节点
|
||||
for item in doc.iterfind('channel/item'):
|
||||
# 使用findtext在节点中寻找需要的数据
|
||||
title = item.findtext('title')
|
||||
date = item.findtext('pubDate')
|
||||
link = item.findtext('link')
|
||||
|
||||
print(title, date, link, sep="\n")
|
Reference in New Issue
Block a user