Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The first part can for the most part be done by <a href="http://docs.python.org/library/xml.etree.elementtree.html" rel="noreferrer">ElementTree</a>, but it takes a few more steps:</p> <pre><code>&gt;&gt;&gt; import xml.etree.ElementTree as ET &gt;&gt;&gt; html = ET.XML('&lt;html&gt;&lt;head&gt;&lt;title&gt;Hi&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;') &gt;&gt;&gt; html.head = html.find('head') &gt;&gt;&gt; html.head.append(ET.XML('&lt;link type="text/css" href="main.css" rel="stylesheet" /&gt;')) &gt;&gt;&gt; html.head.title = html.head.find('title') &gt;&gt;&gt; html.head.title.text 'Hi' </code></pre> <p>The second part can be completed by creating Element objects, but you'd need to do some of your own work to make it happen the way you really want:</p> <pre><code>&gt;&gt;&gt; html.body = html.find('body') &gt;&gt;&gt; my_h1 = ET.Element('h1', {'class': 'roflol'}) &gt;&gt;&gt; my_h1.text = 'BIG TITLE!12' &gt;&gt;&gt; html.body.append(my_h1) &gt;&gt;&gt; html.body.SOURCE = ET.tostring(html.body) &gt;&gt;&gt; html.body.SOURCE '&lt;body&gt;&lt;h1 class="roflol"&gt;BIG TITLE!12&lt;/h1&gt;&lt;/body&gt;' </code></pre> <p>You could create a <code>stylesheet</code> function of your own:</p> <pre><code>&gt;&gt;&gt; def stylesheet(href='', type='text/css', rel='stylesheet', **kwargs): ... elem = ET.Element('link', href=href, type=type, rel=rel) ... return elem ... &gt;&gt;&gt; html.head.append(stylesheet(href="main.css")) </code></pre> <p>And the whole document:</p> <pre><code>&gt;&gt;&gt; ET.tostring(html) &lt;html&gt;&lt;head&gt;&lt;title&gt;Hi&lt;/title&gt;&lt;link href="main.css" rel="stylesheet" type="text/css" /&gt;&lt;/head&gt;&lt;body&gt;&lt;h1 class="roflol"&gt;BIG TITLE!12&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt; </code></pre> <p>But, I think if you're going to end up writing your own thing, this is a good place to start. ElementTree is very powerful.</p> <p><strong>Edit:</strong> I realize that this is probably not exactly what you're looking for. I just wanted to provide something as an available alternative and to also prove that it could actually be done without too much work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload