zoukankan      html  css  js  c++  java
  • 【python】lxml-The E-factory

    来自:http://lxml.de/tutorial.html

    lxml中的E-factory可以用个简单快速的生成XML和HTML

    >>> from lxml.builder import E
    
    >>> def CLASS(*args): # class is a reserved word in Python
    ...     return {"class":' '.join(args)}
    
    >>> html = page = (
    ...   E.html(       # create an Element called "html"
    ...     E.head(
    ...       E.title("This is a sample document")
    ...     ),
    ...     E.body(
    ...       E.h1("Hello!", CLASS("title")),
    ...       E.p("This is a paragraph with ", E.b("bold"), " text in it!"),
    ...       E.p("This is another paragraph, with a", "
          ",
    ...         E.a("link", href="http://www.python.org"), "."),
    ...       E.p("Here are some reservered characters: <spam&egg>."),
    ...       etree.XML("<p>And finally an embedded XHTML fragment.</p>"),
    ...     )
    ...   )
    ... )
    
    >>> print(etree.tostring(page, pretty_print=True))
    <html>
      <head>
        <title>This is a sample document</title>
      </head>
      <body>
        <h1 class="title">Hello!</h1>
        <p>This is a paragraph with <b>bold</b> text in it!</p>
        <p>This is another paragraph, with a
          <a href="http://www.python.org">link</a>.</p>
        <p>Here are some reservered characters: &lt;spam&amp;egg&gt;.</p>
        <p>And finally an embedded XHTML fragment.</p>
      </body>
    </html>
  • 相关阅读:
    Spark学习--SparkCore03
    2D特效和3D特效
    CSS3选择器在HTML5中的使用
    HTML5中表单中新增加元素
    HTML5简介
    机器学习系列:
    机器学习系列:
    机器学习系列:
    机器学习系列:
    机器学习系列:
  • 原文地址:https://www.cnblogs.com/dplearning/p/5680433.html
Copyright © 2011-2022 走看看