Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lxml etree.iterparse error "TypeError: reading file objects must return plain strings"

I would like to parse an HTML document using lxml. I am using python 3.2.3 and lxml 2.3.4 ( http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml )

I am using the etree.iterparse to parse the document, but it returns the following run-time error:

Traceback (most recent call last):
  File "D:\Eclipse Projects\Python workspace\Crawler\crawler.py", line 12, in <module>
    for event, elements in etree.iterparse(some_file_like):
  File "iterparse.pxi", line 491, in lxml.etree.iterparse.__next__ (src/lxml\lxml.etree.c:98565)
  File "iterparse.pxi", line 512, in lxml.etree.iterparse._read_more_events (src/lxml\lxml.etree.c:98768)
TypeError: reading file objects must return plain strings

The question is: How to solve this run-time error?

Thank you very much.

Here is the code:

from io import StringIO
from lxml import etree

some_file_like = StringIO("<root><a>data</a></root>")

for event, elements in etree.iterparse(some_file_like): #<-- Run-time error happens here
    print("%s, %4s, %s" % (event, elements.tag, elements.text))
like image 817
Ababneh A Avatar asked Apr 20 '12 07:04

Ababneh A


1 Answers

Your StringIO buffer has unicode string. iterparse works with file like objects that return bytes. The following buffer should work with iterparse:

from io import BytesIO
some_file_like = BytesIO("<root><a>data</a></root>".encode('utf-8'))
like image 76
Imran Avatar answered Nov 03 '22 11:11

Imran