简体   繁体   English

python lxml.html添加参数

[英]python lxml.html add parameter

I have a html-template where i want to add some content. 我有一个html模板,我想在其中添加一些内容。 The Template looks like the following: 该模板如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Data Base</title>
    <link rel="stylesheet" href="stylesheet.css" />
</head>
<body>
    <h1>Data Base</h1>
    <div class="file_explorer">
    </div>
    <div class="info_screen">
    </div>
</body>
</html>

I want to search for the <div class="file_explorer"></div> and add some parameters to it. 我想搜索<div class="file_explorer"></div>并向其中添加一些参数。 Afterwards it should look like this: 之后,它应如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Data Base</title>
    <link rel="stylesheet" href="stylesheet.css" />
</head>
<body>
    <h1>Data Base</h1>
    <div class="file_explorer">
        <p class="folder">Folder_1</p>
        <p class="folder">Folder_2</p>
    </div>
    <div class="info_screen">
    </div>
</body>
</html>

Therefore I tried to parse the html-template and wanted to search for the file_explorer tag to add the paragraphs. 因此,我尝试解析html模板,并想搜索file_explorer标记以添加段落。 How do I search for them and add the paragraphs afterwards. 如何搜索它们,然后添加段落。 I tried html.cssselector but it did not work. 我尝试了html.cssselector,但是没有用。 Pls help me. 请帮助我。 Thats my code: 那就是我的代码:

from lxml import html
from os import path

class HtmlGenerator:

@staticmethod
def modify_html(html_path, list_folders):
    html_path = path.abspath(html_path)
    parser = html.HTMLParser(remove_blank_text=True)
    if path.isfile(html_path) and html_path.endswith(".html"):
        tree = html.parse(html_path, parser)
        # search for <div class="file_explorer"> [MISSING]
        for folder in list_folders:
            # add folder as paragraph to html [MISSING]
        tree.write(html_path, pretty_print=True)

Thanks in advance. 提前致谢。

You can use XPath to find the target div in your template, and then use E-factory to build the new elements : 您可以使用XPath在模板中找到目标div ,然后使用E-factory构建新元素:

from lxml.html import builder as E
....
tree = html.parse(html_path, parser)
root = tree.getroot()
# search for <div class="file_explorer">
div = root.find('.//div[@class="file_explorer"]')
for folder in list_folders:
    # add folder as paragraph to html
    # I assume `folder` as a string like 'Folder_1', 'Folder_2', ...
    d.append(E.P(E.CLASS('folder'), folder))
tree.write(html_path, pretty_print=True)

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

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