In this assignment you will write a Python program somewhat similar to http://www.py4e.com/code3/geoxml.py. 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.
We provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment.
- Sample data: http://py4e-data.dr-chuck.net/comments_42.xml (Sum=2553)
You do not need to save these files to your folder since your program will read the data directly from the URL. Note: Each student will have a distinct data url for the assignment - so only use your own data url for analysis.
在本作业中,您将编写一个类似于 http://www.py4e.com/code3/geoxml.py的 Python 程序。该程序将提示输入 URL,使用 urllib从该 URL 读取 XML 数据,然后从 XML 数据中解析和提取评论计数,计算文件中数字的总和。
个人代码:
import urllib.request, urllib.parse, urllib.error
import xml.etree.ElementTree as ET
import ssl
url = input("Enter location: ")
print('Retrieving', url)
uh = urllib.request.urlopen(url)
data = uh.read()
print('Retrieved', len(data), 'characters')
tree = ET.fromstring(data)
comment = tree.findall('comments/comment')
summ=0
for item in comment:
summ = summ + int(item.find('count').text)
counts = tree.findall('.//count')
print("Count:",len(counts))
print("Sum:",summ)