2016-03-30 80 views
1

我正在嘗試編寫一個程序,該程序利用urllib2來解析HTML,然後利用PyRSS2Gen創建XML中的RSS源。Python - 用PyRSS2Gen生成RSS

我一直在試圖運行它得到的錯誤

Traceback (most recent call last): File "pythonproject.py", line 46, in get_rss() File "pythonproject.py", line 43, in get_rss rss.write_xml(open("cssnews.rss.xml", "w")) File "build/lib/PyRSS2Gen.py", line 34, in write_xml self.publish(handler) File "build/lib/PyRSS2Gen.py", line 380, in publish item.publish(handler) File "build/lib/PyRSS2Gen.py", line 427, in publish _opt_element(handler, "title", self.title) File "build/lib/PyRSS2Gen.py", line 58, in _opt_element _element(handler, name, obj) File "build/lib/PyRSS2Gen.py", line 53, in _element obj.publish(handler) AttributeError: 'builtin_function_or_method' object has no attribute 'publish'

從我能找到的其他用戶在嘗試爲XML創建新標記時遇到此問題,但我試圖使用PyRSS2Gen給出的默認標記。檢查PyRSS2Gen.py文件顯示我正在使用的write_xml()命令,以及如何通過從列表中彈出它們將值分配給rss項目的錯誤?

def get_rss(): 
    sys.path.append('build/lib') 
    from PyRSS2Gen import RSS2, RSSItem 

    rss = RSS2(
     title = 'Python RSS Creator', 
     link = 'technews.acm.org', 
     description = 'Creates RSS out of HTML', 
     items = [], 
    ) 

    for x in range(0, len(rssTitles)): 
     rss.items.append(RSSItem(
      title = rssTitles.pop, 
      link = rssLinks.pop, 
      description = rssDesc.pop, 
     )) 

    rss.write_xml(open("cssnews.rss.xml", "w")) 

# 5 - Call function 
get_rss() 

回答

0

我最後只是寫了一個文件,像這樣;

news = open("news.rss.xml", "w") 
news.write("<?xml version=\"1.0\" ?>") 
news.write("\n") 
news.write("<rss xmlns:atom=\"http://www.w3.org/2005/Atom\" version=\"2.0\">") 
news.write("\n") 
news.write("<channel>") 
news.write("\n")