2012-12-31 33 views
3
import urllib 
import lxml.html 
down='http://blog.sina.com.cn/s/blog_71f3890901017hof.html' 
file=urllib.urlopen(down).read() 
root=lxml.html.document_fromstring(file) 
body=root.xpath('//div[@class="articalContent "]')[0] 
print body.text_content() 

當我運行代碼時,我得到的是文本內容,我如何獲取它的html源代碼,而不是文本內容?如何在lxml中獲取html的源代碼?

回答

4

使用

html = lxml.html.tostring(node) 

,並請:閱讀的您正在使用第一工具的基本文件。