Python.Extracting Data from XML
2016-07-22 20:12
393 查看
The program will prompt for a URL, read the XML data from that URL using urllib and
then parse and extract the comment counts from the XML data, compute the sum of the numbers in the file.
XML地址:http://python-data.dr-chuck.net/comments_290545.xml (Sum
ends with 57)
Python源码:
import urllib
import xml.etree.ElementTree as ET
url = raw_input('Enter location: ')
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
tree = ET.fromstring(data)
counts = tree.findall('.//comment')
lst = list()
for count in counts:
count = int(count.find('.//count').text)
lst.append(count)
print sum(lst)运行结果:
Enter location: http://python-data.dr-chuck.net/comments_290545.xml Retrieved 4204 characters
2457
then parse and extract the comment counts from the XML data, compute the sum of the numbers in the file.
XML地址:http://python-data.dr-chuck.net/comments_290545.xml (Sum
ends with 57)
Python源码:
import urllib
import xml.etree.ElementTree as ET
url = raw_input('Enter location: ')
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
tree = ET.fromstring(data)
counts = tree.findall('.//comment')
lst = list()
for count in counts:
count = int(count.find('.//count').text)
lst.append(count)
print sum(lst)运行结果:
Enter location: http://python-data.dr-chuck.net/comments_290545.xml Retrieved 4204 characters
2457
相关文章推荐
- Python.Following Links in HTML Using BeautifulSoup
- python使用urllib2抓取防爬取链接
- python-pickle/cPickle/glob/tarfile
- 转载:python使用urllib2抓取防爬取链接
- Numpy 常用函数及读写操作
- Python 关于正负无穷float(‘inf’)的一些用法
- Python之urlparse模块
- python中ascii码和字符的转换
- 利用python如何刷访问量
- python 之编码转换 unicode, utf-8, utf-16, GBK
- python 多线程爬虫
- 记一次安装Ipython的流程
- Python sort系列
- python排序函数sort()、sorted()、argsort()
- 文本处理(python)
- Scrapy:Python的爬虫框架----原理介绍
- python——异常处理
- Python中向excel中写入数据
- python开发_platform_获取操作系统详细信息工具
- python 正则表达式 之re.findall