Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在lxml中获取html的源代码?_Python_Lxml - Fatal编程技术网

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

Python 如何在lxml中获取html的源代码?,python,lxml,Python,Lxml,当我运行代码时,我得到的是文本内容,如何获得它的html源代码,而不是文本内容?使用 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="articalCont

当我运行代码时,我得到的是文本内容,如何获得它的html源代码,而不是文本内容?

使用

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.tostring(node)