> On 12 May 2022, at 10:26, Gilles <[email protected]> wrote: > Had another issue with parse(): It doesn't like reading from a string, so > StringIO() is the way to go: > > """ > Traceback (most recent call last): > File "C:\myscript.py", line 170, in <module> > tree = et.parse(content, parser) > File "src\lxml\etree.pyx", line 3521, in lxml.etree.parse > File "src\lxml\parser.pxi", line 1859, in lxml.etree._parseDocument > File "src\lxml\parser.pxi", line 1885, in lxml.etree._parseDocumentFromURL > File "src\lxml\parser.pxi", line 1789, in lxml.etree._parseDocFromFile > File "src\lxml\parser.pxi", line 1177, in > lxml.etree._BaseParser._parseDocFromFile > File "src\lxml\parser.pxi", line 615, in > lxml.etree._ParserContext._handleParseResultDoc > File "src\lxml\parser.pxi", line 725, in lxml.etree._handleParseResult > File "src\lxml\parser.pxi", line 652, in lxml.etree._raiseParseError > OSError: Error reading file '<html>
Look at the last line above - you're giving parse() a string containing XML data which the parse() function is treating as a filename; trying to open a file with a name equivalent to your XML content! If you want to parse an XML string - use et.fromstring() instead. The StringIO call may be reasonable if your XML didn't exist on disk; but if your source data is on disk best to either give parse() the filename (but then you get your #13 issue) or pass it a file handle provided by open(). Kind regards aid
_______________________________________________ lxml - The Python XML Toolkit mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/lxml.python.org/ Member address: [email protected]
