[Python-de] BeautifulSoup contents Listen error

Andreas Jung lists at andreas-jung.com
Sa Mai 31 12:15:03 CEST 2008



--On 31. Mai 2008 11:06:55 +0200 "A. Nigl" <newsletter at que-world.net> wrote:

> Hallo Liste,
> ich versuche gerade ein eine, naja sagen wir mal, xml-Datei mit
> BeautifulSoup zu parsen und bekomme dabei eine mir unerklärliche
> Fehlermeldung. Es ist nicht so das ich diese nicht umgehen könnte,
> allerdings verwirrt es mich etwas wenn ich aus einer Liste mit der Länge
> 1 nicht auch das erste Element entnehmen kann.
> Aber seht selbst:
>
> from BeautifulSoup import BeautifulStoneSoup
> xml = '<HAM><EGG><HAM_ID><![CDATA[12345]]></HAM_ID></EGG></HAM>'
> soup = BeautifulStoneSoup(xml)
> for egg in soup('egg'):
>      for ham in egg('ham_id'):
>          print type(ham.contents)
>          print len(ham.contents)
>          print ham.contents
>          print ham.contents[0]
>
> ==
>
> Die Ausgabe lautet:
> <type 'list'>
> 1
> [u'12345']
>
> Traceback (most recent call last):
>    File "test.py", line 9, in -toplevel-
>      print ham.contents[0]
> TypeError: 'NoneType' object is not callable

Funktioniert hervorragend bei mir:

ajung at suxmac:~ cat test.py
from BeautifulSoup import BeautifulStoneSoup
xml = '<HAM><EGG><HAM_ID><![CDATA[12345]]></HAM_ID></EGG></HAM>'
soup = BeautifulStoneSoup(xml)
for egg in soup('egg'):
   for ham in egg('ham_id'):
       print type(ham.contents)
       print len(ham.contents)
       print ham.contents
       print ham.contents[0]

ajung at suxmac:~ !py
python2.4 test.py
<type 'list'>
1
[u'12345']
12345


Unabhängig von Deinem Problem: XML parst man mit einem XML Parser - nicht
mir Beautifulsoup - auch wenn es vorgibt in irgendeiner Form XML parsen zu 
können. Nimm bitte einer der vielen verfügbaren XML Parser, die es für 
Python gibt.

Andreas
-------------- nächster Teil --------------
Ein Dateianhang mit Binärdaten wurde abgetrennt...
Dateiname   : nicht verfügbar
Dateityp    : application/pgp-signature
Dateigröße  : 194 bytes
Beschreibung: nicht verfügbar
URL         : <http://mail.python.org/pipermail/python-de/attachments/20080531/55fdd079/attachment.pgp>


Mehr Informationen über die Mailingliste python-de