0
我試圖解析:蟒蛇LXML etree.tostring()爲什麼 r的編碼
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">
<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>
<p><a href="http://2012.qq.com/sports/judo/index.htm" target="_blank">柔道</a></p>
<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>
</li>
它爲什麼
?
每行都有
。爲什麼?