发布时间:2019-09-27 07:07:17编辑:auto阅读(3074)
对于RSS源的XML文件,开头如下:
<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
对于这类xml的解析代码如下:
from xml.etree.ElementTree import parse
# 解析XML文件
doc = parse('d:\\rss20.xml')
# 获取对应的节点
for item in doc.iterfind('channel/item'):
# 获取属性对应的值
title = item.findtext('title')
print(title)
print()
对于常规的XML文件,开头如下:
<?xml version="1.0" encoding="utf-8"?>
对于这类XML文件的解析代码如下:
from xml.etree.ElementTree import parse
# 解析XML
doc = parse('d:\\356.xml')
# 获取根节点
root = doc.getroot()
# 获取根节点下面的下一节点
for data in root.findall('data'):
for report in data.findall('report'):
for targets in report.findall('targets'):
for target in targets.findall('target'):
print('扫描ip:', end='')
# 获取属性对应的值
ip = target.find('ip').text
print(ip)
上一篇: python3中关于时间格式的操作
下一篇: Python3中的open函数
53611
40447
34862
30592
25469
25250
23685
19095
15254
14767
1260°
1169°
1235°
1269°
1288°
1460°
1412°
1372°
1479°
1428°