简体   繁体   中英

python lxml.html add parameter

I have a html-template where i want to add some content. 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. 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. How do I search for them and add the paragraphs afterwards. I tried html.cssselector but it did not work. 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 :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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