2015-11-25 50 views
-2

我一直在考慮的任務,我要提取號碼的開出的this xml file,然後總結他們。問題是,當我嘗試做一個for循環來獲取數據,我得到一個屬性錯誤:如何從這段數據中獲取數字?

TypeError: 'NoneType' object is not callable

這裏是我到目前爲止的代碼:

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> 

有沒有我在這裏失蹤的東西?

回答

1

我建議使用美麗的湯。 http://www.crummy.com/software/BeautifulSoup/bs4/doc/

它使解析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() 
+0

這是我當我運行代碼得到:回溯(最近通話最後一個): 文件「/用戶/阿西夫/桌面/套接字文件/ geoxmlpapa.py」 19行,在 的標籤在soup.find_all(「計數」) : TypeError:'NoneType'對象不可調用。我也得到了nonetype對象的錯誤,這也是它的原始方式。 –

1

我想我的代碼,它成功了!

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 
0

A.K.像Gabor Erdos說的那樣。 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) 
0

對於Python 3,這沒有爭議的代碼應該只是罰款爲您分配:

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 
0
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) 
相關問題