从本地文件解析 HTML
Parse HTML from local file
我将 Google App Engine 与 Python 结合使用。我想从与我的 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()
它returns出现以下错误:
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 脚本是否可以访问该文件。如果您将该文件提供给所有人,它会起作用吗?
我认为您的错误出在您的文件路径中。您假设您的应用程序目录是服务器上文件系统的根目录。这不一定是这种情况。实际上,我找不到任何关于文件位置的文档,所以这就是我所做的(它在开发服务器上工作,我还没有在生产中厌倦它):
我假设 Google 保留了我的应用程序中文件的相对位置。因此,如果我知道一个文件的位置,就可以确定其余文件的位置。幸运的是,python 规范允许您以编程方式确定 python 源文件的位置,如下所示:
def get_src_dir(){
return os.path.dirname(os.path.realpath(__file__))
}
get_src_dir() 你会得到源文件的位置。
os.path.join(get_src_dir(), rel_path_to_asset)
现在将为您提供资产路径。 rel_path_to_asset 是相对于源文件的资产路径 get_src_dir() 函数在...
您的工作目录是您应用程序目录的基础。因此,如果您的应用组织如下:
- app.yaml
- nl/
- home.html
然后您可以在 nl/html.html
阅读您的文件(假设您没有更改您的工作目录)。
我将 Google App Engine 与 Python 结合使用。我想从与我的 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()
它returns出现以下错误: 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 脚本是否可以访问该文件。如果您将该文件提供给所有人,它会起作用吗?
我认为您的错误出在您的文件路径中。您假设您的应用程序目录是服务器上文件系统的根目录。这不一定是这种情况。实际上,我找不到任何关于文件位置的文档,所以这就是我所做的(它在开发服务器上工作,我还没有在生产中厌倦它):
我假设 Google 保留了我的应用程序中文件的相对位置。因此,如果我知道一个文件的位置,就可以确定其余文件的位置。幸运的是,python 规范允许您以编程方式确定 python 源文件的位置,如下所示:
def get_src_dir(){
return os.path.dirname(os.path.realpath(__file__))
}
get_src_dir() 你会得到源文件的位置。
os.path.join(get_src_dir(), rel_path_to_asset)
现在将为您提供资产路径。 rel_path_to_asset 是相对于源文件的资产路径 get_src_dir() 函数在...
您的工作目录是您应用程序目录的基础。因此,如果您的应用组织如下:
- app.yaml
- nl/
- home.html
然后您可以在 nl/html.html
阅读您的文件(假设您没有更改您的工作目录)。