简体   繁体   English

如何获取lxml中的html源代码?

[英]How to get the source of html in lxml?

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()

When i run the code, what i get is the text content ,how can i get the html source code of it,not the text content? 当我运行代码时,我得到的是文本内容,我如何获得它的html源代码,而不是文本内容?

Use 使用

html = lxml.html.tostring(node)

and please: read the basic documentation of the tools you are using first. 并请:先阅读您正在使用的工具的基本文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM