xml - Why does this Python script only read the last RSS post into the file? -
I'm trying to fix a Python script that takes posts from a specific RSS feed and pulls them down And puts them in a text file. As you can see below, there are two main print functions. Once prints the shell only once, but it shows all of posts, which I want to do it. Now, the second part is where the problem lies, it prints the last post of the RSS feed in a single text, not the whole thing, as the first function does. I have also tried to create another function (f = open), as it would be the first time with a new print-line PR instead of% s.
If someone can tell me why the script does not post more than one post (last) of RSS feeds in the text, but there is whole thing in the shell, and what I need to improve, I appreciate it :)
Here is the code:
Import urlib import system import xml.dom.minidom # Feed URL address = 'Http: // Www.vg.no/export/Alle/rdf.hbs?kat=nyheter '#Our actual XML document document = xml.dom.minidom.parse (urllib.urlopen (address)) for the item item.getElements ByTagName ('item'): title = item.getElementsByTagName ('title') [0] .firstChild.data link = item.getElementsByTagName ('link') [0]. First Childdata Description = item.getElementsByTagName ('Description') [0] .firstChild.data str = link.strip ("http://go.vg.no/cgi-bin/go.cgi/rssart/") Print " \ N "Print" ------- -------------------------------------- ------------ --------- "Print" '% s' \ n \ n% s \ n \ n (% s)' ''% (title. Encode ('UTF8', 'Replace'), Description.encode ('UTF8', 'replace'), str.encode ('UTF8', 'replace')) Print "---------- ----------- --------------------------------------- ----- "print" \ n "F = open ('lawl.txt', 'w') print> gt; & gt; F," --------------- ------- NESTH PV VG ------------------------------- "Print> F; Print.encode ('UTF8', 'Replacement') Print & gt; & gt; f, description.encode ('UTF8', 'replace') Print & gt; & gt; f, str.encode (' UTF8 ',' Replacement ') Print & gt; f, "------ ------------------------------ -------------------- ---------- "Print & gt; f," \ n "
< / Div>
your print> f
for
loop Afterwards, they are run once, and you have the last
title
, description
, and str
.
For the loop
then print & gt; & Gt; F / l> Insert the lines inside the loop.
import urlib import system import xml.dom.minidom # feed address = 'http://www.vg.no/export/Alle/rdf.hbs URL? For the items in the document.getElementsByTagName ('item') = Katy = nyheter 'f = open (' lawl.txt ',' w ') #Our actual XML document document = xml.dom.minidom.parse (urllib.urlopen (address)) '): Title = item.getElementsByTagName (' title ') [0] .firstChild.data link = item.getElementsByTagName (' link ') [0]. The first childdata description = item.getElementsByTagName ('description') [0] .firstChild.data str = link.strip ("http://go.vg.no/cgi-bin/go.cgi/rssart/") print " \ N "Print" ---------- ----------------------------------- --------------- ------ "print" '' "% s" \ n \ n% s \ n \ n (% s) '' '% (title .encode ('UTF8', 'Replacement'), Description. Encoded ('UTF8', 'Replaced'), str.encode ('UTF8', 'Replaced')) Print "---------- --------------- ----------------------------------- ------ "Print" \ n "Print & gt; & Gt; F, "---------------------- Senior Pi Vi ------------ ---------- --------- "Print & gt; & Gt; F, title.encode ('UTF8', 'Replacement') Print & gt; & Gt; F, description.encode ('UTF8', 'replace') Print & gt; & Gt; F, str.encode ('UTF8', 'Replacement') Print & gt; & Gt; F, "-------------------------------------------------- ------------------ - "Print & gt; & Gt; F, "\ n"
Comments
Post a Comment