私のデータベースには、次のような値があります。
<p class="description">Text here
<a href=#>Text here</a>
</p>
そして、テンプレートファイルで<p>
同じオブジェクトを置き換えるには、そのオブジェクト全体が必要です。<p class="description>
import sys
from HTMLParser import HTMLParser
from xml.etree import cElementTree as etree
import psycopg2
import psycopg2.extras
class LinksParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.tb = etree.TreeBuilder()
def handle_starttag(self, tag, attributes):
self.tb.start(tag, dict(attributes))
def handle_endtag(self, tag):
self.tb.end(tag)
def handle_data(self, data):
self.tb.data(data)
def close(self):
HTMLParser.close(self)
return self.tb.close()
conn = psycopg2.connect(dbname="**", user="**", password="**", host="/tmp/", port="**")
cur.execute("SELECT * FROM landingpagedata;")
row = cur.fetchone()
template = 'template.html'
parser = LinksParser()
parser.feed(open(template).read())
root = parser.close()
#p_class_description
p = root.find(".//p[@class='description']")
p.text = str(row['p_class_description'])
f = open(row['new_html_page'], 'w')
root.write(f)
parser = LinksParser()
ファイルで最終的に得られるのは次のとおりです。
<p class="suggested_readings"><p class="suggested_readings">Text here <a href=#;">Text here </a>.</p>
<a href=#>Text from template</a> and more from template</p>
要素全体をこの要素に置き換えることはできないようです。または、データベースに実際の要素を保存しようとする必要がありますか? ここではとても無力です。
ありがとう!!!