2013-08-01 53 views
4

我有下面的XML文件:類型錯誤:「NoneType」對象不是可調用的Python用BeautifulSoup XML

<user-login-permission>true</user-login-permission> 
     <total-matched-record-number>15000</total-matched-record-number> 
     <total-returned-record-number>15000</total-returned-record-number> 
     <active-user-records> 
      <active-user-record> 
       <active-user-name>username</active-user-name> 
       <authentication-realm>realm</authentication-realm> 
       <user-roles>Role</user-roles> 
       <user-sign-in-time>date</user-sign-in-time> 
       <events>0</events> 
       <agent-type>text</agent-type> 
       <login-node>node</login-node> 
      </active-user-record> 

有許多記錄 我試圖從標籤獲取值,並將其保存在不同的文本使用以下代碼文件:

soup = BeautifulSoup(open("path/to/xmlfile"), features="xml") 


with open('path/to/outputfile', 'a') as f: 
    for i in range(len(soup.findall('active-user-name'))): 
     f.write ('%s\t%s\t%s\t%s\n' % (soup.findall('active-user-name')[i].text, soup.findall('authentication-realm')[i].text, soup.findall('user-roles')[i].text, soup.findall('login-node')[i].text)) 

我得到的錯誤類型錯誤:「NoneType」對象不可調用Python和BeautifulSoup XML爲線:對於i在範圍(LEN(soup.findall(「活性用戶名」 ))):

任何想法可能會導致這種情況?

謝謝!

回答

5

有很多問題需要解決,首先是您提供的XML文件不是有效的XML - 需要一個根元素。

嘗試這樣的事情作爲XML:

<root> 
    <user-login-permission>true</user-login-permission> 
    <total-matched-record-number>15000</total-matched-record-number> 
    <total-returned-record-number>15000</total-returned-record-number> 
    <active-user-records> 

     <active-user-record> 
      <active-user-name>username</active-user-name> 
      <authentication-realm>realm</authentication-realm> 
      <user-roles>Role</user-roles> 
      <user-sign-in-time>date</user-sign-in-time> 
      <events>0</events> 
      <agent-type>text</agent-type> 
      <login-node>node</login-node> 
     </active-user-record> 

    </active-user-records> 
</root> 

現在到了蟒蛇。首先沒有findall方法,它可以是findAllfind_allfindAllfind_all是等價的,如記錄here

接下來我會建議改變你的代碼,這樣你就不會利用該find_all方法的這麼頻繁 - 使用find反而會提高效率,特別是大型的XML文件。此外,下面的代碼更容易閱讀和調試:

from bs4 import BeautifulSoup 

xml_file = open('./path_to_file.xml', 'r') 

soup = BeautifulSoup(xml_file, "xml") 

with open('./path_to_output_f.txt', 'a') as f: 
    for s in soup.findAll('active-user-record'): 
     username = s.find('active-user-name').text 
     auth = s.find('authentication-realm').text 
     role = s.find('user-roles').text 
     node = s.find('login-node').text 
     f.write("{}\t{}\t{}\t{}\n".format(username, auth, role, node)) 

希望這會有所幫助。讓我知道你是否需要任何進一步的幫助!

+0

優化我的腳本2S,謝謝! – user2633192

1

解決方法很簡單:不要使用findall方法 - 使用find_all

爲什麼?因爲根本沒有findall方法,所以有findAllfind_all,它們是等效的。有關更多信息,請參閱docs

雖然,我同意,錯誤信息是混亂。

希望有所幫助。

+0

謝謝你,與find_all取代的findall和它做什麼,我想 – user2633192

0

我對這個問題的解決方案是將BeautifulSoup實例強制轉換爲類型字符串。你這樣做如下: https://groups.google.com/forum/#!topic/comp.lang.python/ymrea29fMFI

您使用以下Python的

: 從蟒蛇人工

STR([對象])

返回一個包含 對象的一個​​很好的打印表示形式的字符串。對於字符串,這會返回字符串本身。與repr(對象)的區別 是str(object)並不總是嘗試 返回eval()可接受的字符串;其目標是返回一個 可打印的字符串。如果沒有給出參數,返回空字符串,

相關問題