python lxml etree.tostring()为什么\ r \ n编码为

时间:2016-05-13 05:36:50

标签: python lxml

我试图解析:

    request = urllib2.Request(url="http://2012.qq.com/sports/")
    response = urllib2.urlopen(request)
    content = response.read()
    uni_content = content.decode("gb2312", "ignore")
    tecent = uni_content.encode("utf-8")

    tecent_page = etree.HTML(tecent, parser=etree.HTMLParser(encoding='utf-8'))
    test_tags = tecent_page.xpath("/html/body/div[@class='page']/div[@class='layout']/div/div[@class='bd']/ul[@class='list']/li")

    for i, item in enumerate(test_tags):
        content = etree.tostring(item, encoding="utf-8", pretty_print=True)
        print content

为什么会这样结果:

<li class="item">&#13;
                        <a class="pic" href="http://2012.qq.com/sports/judo/index.htm" target="_blank"><img width="96" height="96" src="http://mat1.gtimg.com/2012/samanthasun/allevents/roudao.png" alt="柔道"/></a>&#13;
                        <p><a href="http://2012.qq.com/sports/judo/index.htm" target="_blank">柔道</a></p>&#13;
                        <p><a href="http://2012.qq.com/l/sports/judo/judochn/list2011079114946.htm" target="_blank">新闻</a> | <a href="http://2012.qq.com/l/photos/33xiangmu/roudao/list2011079115124.htm" target="_blank">图片</a> | <a href="http://2012.qq.com/l/video/xm/vjudo/list.htm" target="_blank">视频</a></p>&#13;
                    </li>&#13;

为什么它有&#13;
每行都有&#13;。为什么呢?

1 个答案:

答案 0 :(得分:0)

因为原始文档(http://2012.qq.com/sports/)有CR+LF line breaksCarriage return代码为13。

您可以使用简单的解决方法:tecent = uni_content.encode("utf-8").replace('\r\n', '\n')