python 过滤和抽取 XML 2.5 passed
时间:2007-03-05 来源:maojj
#!/usr/bin/env python
import xml.etree.ElementTree
weblog = xml.etree.ElementTree.parse("C:\Script\modifyCfg\weblog.xml").getroot()
print weblog
aa = weblog.findall('entry')
print aa
for entry in weblog.findall("entry"):
if entry.find('statusCode').text == '200':
print entry
interesting = [entry
for entry in weblog.findall('entry')
if entry.find('host').text == '64.172.22.154' and entry.find('statusCode').text == '200']
for e in interesting:
print "%s (%s)"% (e.findtext('resource'), e.findtext('byteCount')) 注:这个例子里有个在[]中循环 weblog.xml <xml>
<entry>
<host>64.172.22.154</host>
<referer>-</referer>
<userAgent>-</userAgent>
<dateTime>19/Aug/2001:01:46:01</dateTime>
<reqID>-0500</reqID>
<reqType>GET</reqType>
<resource>/</resource>
<protocol>HTTP/1.1</protocol>
<statusCode>200</statusCode>
<byteCount>2131</byteCount>
</entry>
<entry>
<host>64.172.22.154</host>
<referer>-</referer>
<userAgent>-</userAgent>
<dateTime>19/Aug/2001:01:46:01</dateTime>
<reqID>-0500</reqID>
<reqType>GET</reqType>
<resource>/</resource>
<protocol>HTTP/1.1</protocol>
<statusCode>200</statusCode>
<byteCount>2131</byteCount>
</entry>
</xml> 注:xml得有个顶层标签,不然会出错
if entry.find('statusCode').text == '200':
print entry
interesting = [entry
for entry in weblog.findall('entry')
if entry.find('host').text == '64.172.22.154' and entry.find('statusCode').text == '200']
for e in interesting:
print "%s (%s)"% (e.findtext('resource'), e.findtext('byteCount')) 注:这个例子里有个在[]中循环 weblog.xml <xml>
<entry>
<host>64.172.22.154</host>
<referer>-</referer>
<userAgent>-</userAgent>
<dateTime>19/Aug/2001:01:46:01</dateTime>
<reqID>-0500</reqID>
<reqType>GET</reqType>
<resource>/</resource>
<protocol>HTTP/1.1</protocol>
<statusCode>200</statusCode>
<byteCount>2131</byteCount>
</entry>
<entry>
<host>64.172.22.154</host>
<referer>-</referer>
<userAgent>-</userAgent>
<dateTime>19/Aug/2001:01:46:01</dateTime>
<reqID>-0500</reqID>
<reqType>GET</reqType>
<resource>/</resource>
<protocol>HTTP/1.1</protocol>
<statusCode>200</statusCode>
<byteCount>2131</byteCount>
</entry>
</xml> 注:xml得有个顶层标签,不然会出错
相关阅读 更多 +