Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何从这段数据中得到数字?_Python_Xml_Python 2.7_Urllib_Elementtree - Fatal编程技术网

Python 我如何从这段数据中得到数字?

Python 我如何从这段数据中得到数字?,python,xml,python-2.7,urllib,elementtree,Python,Xml,Python 2.7,Urllib,Elementtree,我被分配了一个任务,我必须从中提取数字,然后把它们加起来。问题是,当我尝试执行for循环以获取数据时,我遇到了一个属性错误: TypeError:“非类型”对象不可调用 以下是我目前的代码: import urllib import xml.etree.ElementTree as ET url = raw_input('Enter location: ') print 'Retrieving', url uh = urllib.urlopen(url) data = uh.read() pr

我被分配了一个任务,我必须从中提取数字,然后把它们加起来。问题是,当我尝试执行for循环以获取数据时,我遇到了一个属性错误:

TypeError:“非类型”对象不可调用

以下是我目前的代码:

import urllib
import xml.etree.ElementTree as ET

url = raw_input('Enter location: ')
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
tree = ET.fromstring(data)
lst = tree.findall('.//count')
print 'Count:', len(lst)
for item in lst:
    print 'name', item.find('count').text 
我应该从计数标记中提取文本:

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

马蒂亚斯
97

这里有什么我不知道的吗?

我建议用漂亮的汤

它使解析xml文件变得非常容易

from bs4 import BeautifulSoup
soup = BeautifulSoup(data)  # It seems that your data variable holds the xml
for tag in soup.find_all('count'):
    print tag.get_text()

我尝试了我的代码,它成功了

   import urllib
   import xml.etree.ElementTree as ET

   url = raw_input('Enter location: ')
   print 'Retrieving', url
   uh = urllib.urlopen(url)
   data = uh.read()
   print 'Retrieved',len(data),'characters'
   tree = ET.fromstring(data)
   lst = tree.findall('.//count')
   print 'Count:', len(lst)
   total = 0
   for comment in tree.findall("./comments/comment"):
       total += int(comment.find('count').text)
   print total

A.K.就像加博·鄂尔多斯所说的。BS4更好,但如果您想使用ElementTree:

import urllib
import xml.etree.ElementTree as ET
url = 'http://python-data.dr-chuck.net/comments_208135.xml'
tresc = urllib.urlopen(url).read()
tree = ET.fromstring(tresc)

wartosci = tree.findall('.//count')
sum = 0
count = 0

for item in wartosci:
    x = int(item.text)
    sum = sum + x
    count = count + 1

print('Retrieving', url)
print('Retrieving'), len(tresc), 'characters'
print('Count: ', count)
print('Sum: ', sum)

对于Python3,这段简单的代码应该适合您的任务:

import urllib.request 
import xml.etree.ElementTree as ET

url = input ("Enter url: ")
response = urllib.request.urlopen(urllib.request.Request(url)).read() 

tree = ET.fromstring(response)
data = tree.findall('.//count')

print (sum( [ int(i.text) for i in data ] )) #int before sum before print

这是我在运行代码时得到的结果:Traceback(最近一次调用):File“/Users/Asif/Desktop/socket File/geoxmlpapa.py”,第19行,for-tag in-soup.find_-all('count'):TypeError:'NoneType'对象不可调用。我得到了非类型对象错误,以原来的方式做它。
import urllib.request, urllib.parse, urllib.error
import xml.etree.ElementTree as ET

url = 'http://py4e-data.dr-chuck.net/comments_4772.xml'
print ('Retrieving', url)
uh = urllib.request.urlopen(url)
data = uh.read()
print('Retrieved', len(data), 'characters')
tree = ET.fromstring(data)
counts = tree.findall('.//count')
print ('Count',len(counts))
total=0
for item in counts:
    element=item.text
    total+=int(element)
print(total)