zsl-oo7

lxml 中文乱码

1、要保证传给lxml的参数都是unicode

2、用 urlopen() 抓到的 file-like object ,或者用open()打开的硬盘上的 file object 不一定是unicode

3、用 unicode(file-like-object.read(),"utf-8") 能得到肯定是unicode的东西

4、这样处理之后再传给lxml的fromstring

5、xml.etree.ElementTree 也是一样

6、虽然lxml.html.parse()可以接受file-like object 作为参数,但是不要用,因为你传进去一个file-like object 你也不知道是不是unicode,万一有中文就会有乱码。

7、总是用unicode(file-like-object.read(),"utf-8") 这么转换对性能肯定是不好,但目前我也只会这种笨方法

评论