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


     

    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
        
  • 相关阅读:
    博客推荐
    oracle11g dataguard 安装手册(转)
    Linux文件系统介绍(转)
    降低磁盘IO使Oracle性能优化(转)
    关于AWR报告命中率指标的解释(转)
    oracle 锁的介绍 (转)
    oracle undo redo 解析
    oracle sqlplus 连接不正常
    Oracle技术嘉年华
    断线的回忆
  • 原文地址:https://www.cnblogs.com/Gailsunset/p/5588863.html
Copyright © 2020-2023  润新知