2013-08-01 52 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()可接受的字符串;其目标是返回一个 可打印的字符串。如果没有给出参数,返回空字符串,

相关问题