码迷,mamicode.com
首页 > 编程语言 > 详细

使用Python访问网络数据 python network-data 第五章

时间:2016-06-15 22:02:33      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:

 

Lesson 5--Extracting Data from XML

In this assignment you will write a Python program somewhat similar tohttp://www.pythonlearn.com/code/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 and enter the sum,

Extracting Data from XML

In this assignment you will write a Python program somewhat similar to http://www.pythonlearn.com/code/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.

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.

 

Data Format and Approach

The data consists of a number of names and comment counts in XML as follows:

<comment>
  <name>Matthias</name>
  <count>97</count>
</comment>

You are to look through all the <comment> tags and find the <count> values sum the numbers. The closest sample code that shows how to parse XML is geoxml.py. But since the nesting of the elements in our data is different than the data we are parsing in that sample code you will have to make real changes to the code.

 

To make the code a little simpler, you can use an XPath selector string to look through the entire tree of XML for any tag named ‘count‘ with the following line of code:

counts = tree.findall(‘.//count‘)

Take a look at the Python ElementTree documentation and look for the supported XPath syntax for details. You could also work from the top of the XML down to the comments node and then loop through the child nodes of the comments node.

 

Sample Execution

 

$ python solution.py 
Enter location: http://python-data.dr-chuck.net/comments_42.xml
Retrieving http://python-data.dr-chuck.net/comments_42.xml
Retrieved 4204 characters
Count: 50
Sum: 2...





import urllib
import xml.etree.ElementTree as ET

#serviceurl = ‘http://maps.googleapis.com/maps/api/geocode/xml?‘

while True:
    url = raw_input(Enter location: )
    #url = ‘http://python-data.dr-chuck.net/comments_42.xml‘
    if len(url) < 1 : break
    print Retrieving, url
    uh = urllib.urlopen(url)
    data = uh.read()
    print Retrieved,len(data),characters

    tree = ET.fromstring(data)

    allNode = tree.findall(.//count)

    print count: , len(allNode)

    sum = 0
    for node in allNode:

        number = int(node.text)
        sum = sum + number

    print sum:, sum

    break
    

 

使用Python访问网络数据 python network-data 第五章

标签:

原文地址:http://www.cnblogs.com/Gailsunset/p/5588863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!