在我的数据库中,我有这样的值:
<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>
我似乎无法用这个元素替换整个元素。或者,我应该尝试在我的数据库中存储一个实际元素吗?在这里很无奈。
谢谢!!!