【发布时间】:2015-07-15 15:58:21
【问题描述】:
我正在使用带有 Python 的 Google App Engine。我想从与我的 Python 脚本相同的项目中获取 HTML 文件的树。我尝试了很多东西,比如使用绝对 url(例如http://localhost:8080/nl/home.html)和相对 url(/nl/home.html)。两者似乎都不起作用。我使用此代码:
class HomePage(webapp2.RequestHandler):
def get(self):
path = self.request.path
htmlfile = etree.parse(path)
template = jinja_environment.get_template('/nl/template.html')
pagetitle = htmlfile.find(".//title").text
body = htmlfile.get_element_by_id("body").toString()
它返回以下错误: IOError:读取文件“/nl/home.html”时出错:无法加载外部实体“/nl/home.html
有谁知道如何使用 Python 从同一个项目中获取 HTML 文件的树?
编辑
这是工作代码:
class HomePage(webapp2.RequestHandler):
def get(self):
path = self.request.path.replace("/","",1)
logging.info(path)
htmlfile = html.fromstring(urllib.urlopen(path).read())
template = jinja_environment.get_template('/nl/template.html')
pagetitle = htmlfile.find(".//title").text
body = innerHTML(htmlfile.get_element_by_id("body"))
def innerHTML(node):
buildString = ''
for child in node:
buildString += html.tostring(child)
return buildString
【问题讨论】:
标签: python html google-app-engine lxml