Using Python to access network data Python network-data chapter fifth

Source: Internet
Author: User
Tags xpath

Lesson 5--extracting Data from XML

In this assignment you'll write a Python program somewhat similar tohttp://www.pythonlearn.com/code/geoxml.py. The program would prompt for a URL, read the XML data from the URL using urllib and then parse and extract the CO Mment 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'll write a Python program somewhat similar to http://www.pythonlearn.com/code/geoxml.py. The program would prompt for a URL, read the XML data from the URL using Urllib and then parse and extract the comment cou NTS from the XML data, compute the sum of the numbers in the file.

We provide the files for this assignment. One is a sample file where do we give you the sum for your testing and the other are the actual data you need to process for T He assignment.

    • Sample Data:http://python-data.dr-chuck.net/comments_42.xml (sum=2553)
    • Actual Data:http://python-data.dr-chuck.net/comments_265442.xml (Sum ends with 66)

You don't need to save these files to your folder since your program would read the data directly from the URL. Note:each student'll has 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 is to look through all the <comment> tags and find the <count> values sum the numbers. The closest sample code that shows what to parse XML is geoxml.py. But since the nesting of the elements in our data is different than the data we be parsing in so sample code you'll h Ave 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. Could also work from the top of the XML down to the comments node and then loop through the child nodes of the comment S node.

Sample execution

$ python solution.py Enter location:http://python-data.dr-chuck.net/comments_42.xmlretrieving/http/ Python-data.dr-chuck.net/comments_42.xmlretrieved 4204 Characterscount:50sum:2 ...





ImportUrllibImportXml.etree.ElementTree as ET#serviceurl = ' http://maps.googleapis.com/maps/api/geocode/xml? ' whileTrue:url= Raw_input ('Enter Location:')    #url = ' Http://python-data.dr-chuck.net/comments_42.xml '    ifLen (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 forNodeinchAllnode:number=int (node.text) sum= Sum + NumberPrint 'sum:', Sum Break    

Using Python to access network data Python network-data chapter fifth

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.