Skip to content

Instantly share code, notes, and snippets.

@netdesign
Created September 24, 2011 17:05
Show Gist options
  • Select an option

  • Save netdesign/1239553 to your computer and use it in GitHub Desktop.

Select an option

Save netdesign/1239553 to your computer and use it in GitHub Desktop.
python lxml.etree.iterparse
import lxml.etree as tr
context = tr.iterparse('content.rdf.u8', events=('end',), tag='{http://dmoz.org/rdf/}ExternalPage')
for event, elem in context:
print('%s' % elem[0].text.encode('utf-8'))
print('%s' % elem[1].text.encode('utf-8'))
print(elem.attrib['about']+"\n")
#print('%s\n' % elem[1].text.encode('utf-8'))
elem.clear()
while elem.getprevious() is not None:
del elem.getparent()[0]
Traceback (most recent call last):
File "parser.py", line 6, in <module>
print('%s' % elem[1].text.encode('utf-8'))
AttributeError: 'NoneType' object has no attribute 'encode'
<ExternalPage about="http://www.castles99.ukprint.com/Essays/stapleton.html">
<d:Title>Stapleton Castle, Herefordshire</d:Title>
<d:Description></d:Description>
<topic>Top/Arts/Architecture/Building_Types/Fortifications/Castles/United_Kingdom/England</topic>
</ExternalPage>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment