python Coursera - 使用XML打开网站并解析数字并总结它们。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Coursera - 使用XML打开网站并解析数字并总结它们。相关的知识,希望对你有一定的参考价值。

import urllib
import xml.etree.ElementTree as ET
numbers = list()
link = 'http://python-data.dr-chuck.net/comments_184196.xml'
soup = urllib.urlopen(link).read()
stuff = ET.fromstring(soup)
lst = stuff.findall('comments/comment')
for item in lst:
    value = item.find('count').text
    numbers.append(value)
numbers = ([int(i) for i in numbers])
print sum(numbers)

以上是关于python Coursera - 使用XML打开网站并解析数字并总结它们。的主要内容,如果未能解决你的问题,请参考以下文章