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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用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
    

 

以上是关于使用Python访问网络数据 python network-data 第五章的主要内容,如果未能解决你的问题,请参考以下文章

吴裕雄 python 神经网络——TensorFlow 数据集基本使用方法

使用神经网络来识别手写数字译- 用Python代码实现

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

python处理网络数据

无需包装器或使用 API 访问 Google 趋势数据:Python

Python网络爬虫---使用已登录的cookie访问需要登录的网页