2016-06-23 3 views
1

피드 RSS (XML)를 생성하는 간단한 Python 스크립트를 개발 중입니다.피드에 Python과 XML 추가 RSS

하지만 나는 항상 최신 뉴스를 먼저 가지고 있기 때문에 이전 피드보다 새 피드를 추가 할 수 없습니다.

내가 초기 XML 파일을 생성하는 데 사용하는 코드입니다 :

#!/usr/bin/env python 
from lxml import etree as ET 

root = ET.Element("rss") 
root.set("version", "2.0") 

channel = ET.SubElement(root, "channel") 

title = ET.SubElement(channel, "title") 
title.text = "W3Schools Home Page" 

link = ET.SubElement(channel, "link") 
link.text = "http://www.w3schools.com" 

description = ET.SubElement(channel, "description") 
description.text = "Free web building tutorials" 

item = ET.SubElement(channel, "item") 

title = ET.SubElement(item, "title") 
title.text = "RSS Tutorial" 

link = ET.SubElement(item, "link") 
link.text = "http://www.w3schools.com/xml/xml_rss.asp" 

description = ET.SubElement(item, "description") 
description.text = "New RSS tutorial on W3Schools" 

print ET.tostring(root, pretty_print=True, xml_declaration=True) 
#write to file: 
tree = ET.ElementTree(root) 
tree.write('feed.xml', pretty_print=True, xml_declaration=True) 

을 그리고 이것은 출력 :

<?xml version='1.0' encoding='ASCII'?> 
<rss version="2.0"> 
    <channel> 
    <title>W3Schools Home Page</title> 
    <link>http://www.w3schools.com</link> 
    <description>Free web building tutorials</description> 
    <item> 
     <title>RSS Tutorial</title> 
     <link>http://www.w3schools.com/xml/xml_rss.asp</link> 
     <description>New RSS tutorial on W3Schools</description> 
    </item> 
    </channel> 
</rss> 

하지만이있는 새 XML 값을 추가하려고 할 수있는 경우 코드 :

:

#!/usr/bin/env python 
from lxml import etree as ET 
parser = ET.XMLParser(remove_blank_text=True) 
tree = ET.parse("feed.xml", parser) 
channel = tree.getroot() 

item = ET.SubElement(channel, "item") 

title = ET.SubElement(item, "title") 
title.text = "Second Insert" 
link = ET.SubElement(item, "link") 
link.text = "http://second.test" 
description = ET.SubElement(item, "description") 
description.text = "description2" 

channel[0].append(item) 

print ET.tostring(channel, pretty_print=True, xml_declaration=True) 
tree = ET.ElementTree(channel) 
tree.write("feed.xml", pretty_print=True, xml_declaration=True) 

결과는 이것이다

<?xml version='1.0' encoding='ASCII'?> 
<rss version="2.0"> 
    <channel> 
    <title>W3Schools Home Page</title> 
    <link>http://www.w3schools.com</link> 
    <description>Free web building tutorials</description> 
    <item> 
     <title>RSS Tutorial</title> 
     <link>http://www.w3schools.com/xml/xml_rss.asp</link> 
     <description>New RSS tutorial on W3Schools</description> 
    </item> 
    **<item> 
     <title>Second Insert</title> 
     <link>http://second.test</link> 
     <description>description2</description> 
    </item>** 
    </channel> 
</rss> 

하지만 좀하고 싶습니다은 :

<?xml version='1.0' encoding='ASCII'?> 
<rss version="2.0"> 
    <channel> 
    <title>W3Schools Home Page</title> 
    <link>http://www.w3schools.com</link> 
    <description>Free web building tutorials</description> 
    **<item> 
     <title>Second Insert</title> 
     <link>http://second.test</link> 
     <description>description2</description> 
    </item>** 
    <item> 
     <title>RSS Tutorial</title> 
     <link>http://www.w3schools.com/xml/xml_rss.asp</link> 
     <description>New RSS tutorial on W3Schools</description> 
    </item> 
    </channel> 
</rss> 

나는 여러 가지 시도를하지만, 내가 틀렸다 위치를 이해하지 않습니다.

누가 나를 도와 드릴까요?

channel.find(".//description").addnext(item) 

print ET.tostring(channel, pretty_print=True, xml_declaration=True) 

또는 이전을 당신이 할 수있는 첫번째 항목에 :

당신은 당신이 addnext로 할 수있는 설명 노드, 후를 추가 할 당신에게 안드레아

답변

0

감사 addprevious :

channel.find(".//item").addprevious(item) 

모두 당신을 줄 것이다 :

<?xml version='1.0' encoding='ASCII'?> 
<rss version="2.0"> 
    <channel> 
    <title>W3Schools Home Page</title> 
    <link>http://www.w3schools.com</link> 
    <description>Free web building tutorials</description> 
    <item> 
     <title>Second Insert</title> 
     <link>http://second.test</link> 
     <description>description2</description> 
    </item> 
    <item> 
     <title>RSS Tutorial</title> 
     <link>http://www.w3schools.com/xml/xml_rss.asp</link> 
     <description>New RSS tutorial on W3Schools</description> 
    </item> 
    </channel> 
</rss> 

channel[0]채널 노드가되도록에 추가 단지 마지막에 새로운 노드를 추가 할 예정이다.

+0

감사합니다.;) –

관련 문제