2
내 로그를보고 Postgres에서 돌아 오는 행이 문자열에서 요소로 바뀌 었음을 어떻게 표시하는지 확인합니다 (그리고 문자열을 인쇄하고 요소를 인쇄합니다 , isElement 부울을 인쇄하십시오!) 그리고 추가하려고 할 때 오류는 요소가 아니라는 것입니다. 허프, 퍼프.Python eTree Parser가 요소를 추가하지 않음
import sys
from HTMLParser import HTMLParser
from xml.etree import cElementTree as etree
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element, SubElement, tostring
import psycopg2
import psycopg2.extras
def main():
# Connect to an existing database
conn = psycopg2.connect(dbname="**", user="**", password="**", host="/tmp/", port="**")
# Open a cursor to perform database operations
cur = conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
cur.execute("SELECT * FROM landingpagedata;")
rows = cur.fetchall()
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()
template = 'template.html'
# parser.feed(open('landingIndex.html').read()) #for testing
# root = parser.close()
for row in rows:
parser = LinksParser()
parser.feed(open(template).read())
root = parser.close()
#title
title = root.find(".//title")
title.text = row['title']
#headline
h1_id_headline = root.find(".//h1")
h1_id_headline.text = row['h1_id_headline']
# print row['h1_id_headline']
#intro
p_class_intro = root.find(".//p[@class='intro']")
p_class_intro.text = row['p_class_intro']
# print row['p_class_intro']
여기서 문제가 발생합니다.
from xml.etree import cElementTree as etree
import xml.etree.ElementTree as ET
croot = etree.Element('root')
child = ET.Element('child')
croot.append(child)
# TypeError: must be Element, not Element
문제의 근본 원인은 우리의 cElementTree
구현을 혼합하는 것입니다 :
{background: url(/images/courses/azRealEstate.png) center no-repeat;}
<Element 'div' at 0x10a999720>
<p class="recommended_background">Materials are are aimed to all aspiring real estate sales associates who wish to obtain the Arizona Real Estate Salesperson license, which is provided by the <a href="http://www.re.state.az.us/" style="text-decoration: underline;">Arizona Department of Real Estate</a>.</p>
True
Traceback (most recent call last):
File "/Users/Morgan13/Programming/LandingPageBuilder/landingPages/landingBuilderTest.py", line 108, in <module> main()
File "/Users/Morgan13/Programming/LandingPageBuilder/landingPages/landingBuilderTest.py", line 84, in main
p_class_recommendedbackground.append(newElement)
TypeError: must be Element, not Element
[Finished in 0.1s with exit code 1]
오우 와우. 절대로 두 번 만난다. 아주 잘 넣어! 감사. – morgs32