2013-05-09 26 views
2

看看我的日誌,看看它是如何說我從Postgres回來的行已經從一個字符串轉換爲一個元素(我打印字符串,打印元素,打印isElement布爾值!),但是當我嘗試追加它時,錯誤在於它不是一個元素。吹,吹。Python的eTree解析器不追加一個元素

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'] 

這裏是問題發生的地方!

 #recommended 
     p_class_recommendedbackground = root.find(".//div[@class='recommended_background_div']") 
     print p_class_recommendedbackground 
     p_class_recommendedbackground.clear() 
     newElement = ET.fromstring(row['p_class_recommendedbackground']) 
     print row['p_class_recommendedbackground'] 
     print ET.iselement(newElement) 
     p_class_recommendedbackground.append(newElement) 

     html = tostring(root) 
     f = open(row['page_name'], 'w').close() 
     f = open(row['page_name'], 'w') 
     f.write(html) 
     f.close() 
     # f = '' 
     # html = '' 
     parser.reset() 
     root = '' 

    # Close communication with the database 
    cur.close() 
    conn.close() 

if __name__ == "__main__": 
    main() 

我的日誌是這樣的:

{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] 

回答

1

我可以重現該錯誤信息是這樣的:

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實施ElementTreexml.etree.ElementTree執行ElementTree。從來沒有兩個人見面。

所以解決方法是簡單地選擇一個,比如說etree,並替換所有出現的另一個(例如用etree代替ET)。

+0

哦,哇。從來沒有兩個人見面。很好!謝謝。 – morgs32 2013-05-09 18:28:17