为什么这个Python脚本只将最后一篇RSS文章读取到文件中?
我正在尝试修复一个Python脚本,它从特定的RSS提要中获取帖子,并将其剥离并输入到文本文件中。正如您在下面看到的,有两个主要的打印功能。一个只打印shell一次,但它显示了所有的帖子,这就是我想要它做的。现在,第二部分是问题所在。它只将RSS提要的最后一篇文章打印到文本中,而不是像第一个函数那样将整个内容打印出来。我还尝试使用%s使第二个函数(f=open())与第一个函数相同,而不是使用新的print line pr.变量 如果有人能告诉我为什么脚本没有在文本中发布多个(最后一个)RSS提要,而是将整个内容放在shell中,以及我需要进行哪些修改来修复它,我将非常感激:) 代码如下:为什么这个Python脚本只将最后一篇RSS文章读取到文件中?,python,xml,rss,Python,Xml,Rss,我正在尝试修复一个Python脚本,它从特定的RSS提要中获取帖子,并将其剥离并输入到文本文件中。正如您在下面看到的,有两个主要的打印功能。一个只打印shell一次,但它显示了所有的帖子,这就是我想要它做的。现在,第二部分是问题所在。它只将RSS提要的最后一篇文章打印到文本中,而不是像第一个函数那样将整个内容打印出来。我还尝试使用%s使第二个函数(f=open())与第一个函数相同,而不是使用新的print line pr.变量 如果有人能告诉我为什么脚本没有在文本中发布多个(最后一个)RSS提
import urllib
import sys
import xml.dom.minidom
#The url of the feed
address = 'http://www.vg.no/export/Alle/rdf.hbs?kat=nyheter'
#Our actual xml document
document = xml.dom.minidom.parse(urllib.urlopen(address))
for item in document.getElementsByTagName('item'):
title = item.getElementsByTagName('title')[0].firstChild.data
link = item.getElementsByTagName('link')[0].firstChild.data
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 >>f, "----------------------Nyeste paa VG-------------------------------"
print >>f, title.encode('UTF8','replace')
print >>f, description.encode('UTF8','replace')
print >>f, str.encode('UTF8','replace')
print >>f, "------------------------------------------------------------------"
print >>f, "\n"
您的
print>>f
在for
循环之后,因此它们运行一次,并对上次保存到title
、description
和str
的数据进行操作
您应该在for
循环之前打开文件,然后将print>>f
行放入循环中
import urllib
import sys
import xml.dom.minidom
#The url of the feed
address = 'http://www.vg.no/export/Alle/rdf.hbs?kat=nyheter'
f = open('lawl.txt','w')
#Our actual xml document
document = xml.dom.minidom.parse(urllib.urlopen(address))
for item in document.getElementsByTagName('item'):
title = item.getElementsByTagName('title')[0].firstChild.data
link = item.getElementsByTagName('link')[0].firstChild.data
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"
print >>f, "----------------------Nyeste paa VG-------------------------------"
print >>f, title.encode('UTF8','replace')
print >>f, description.encode('UTF8','replace')
print >>f, str.encode('UTF8','replace')
print >>f, "------------------------------------------------------------------"
print >>f, "\n"
您的
print>>f
在for
循环之后,因此它们运行一次,并对上次保存到title
、description
和str
的数据进行操作
您应该在for
循环之前打开文件,然后将print>>f
行放入循环中
import urllib
import sys
import xml.dom.minidom
#The url of the feed
address = 'http://www.vg.no/export/Alle/rdf.hbs?kat=nyheter'
f = open('lawl.txt','w')
#Our actual xml document
document = xml.dom.minidom.parse(urllib.urlopen(address))
for item in document.getElementsByTagName('item'):
title = item.getElementsByTagName('title')[0].firstChild.data
link = item.getElementsByTagName('link')[0].firstChild.data
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"
print >>f, "----------------------Nyeste paa VG-------------------------------"
print >>f, title.encode('UTF8','replace')
print >>f, description.encode('UTF8','replace')
print >>f, str.encode('UTF8','replace')
print >>f, "------------------------------------------------------------------"
print >>f, "\n"
您迭代所有帖子,将它们的属性分配给变量并打印到终端 然后将变量(恰好包含上次赋值的结果)打印到文件中。所以你在这里有一个帖子
如果您想要多个帖子,也需要迭代。您迭代所有帖子,将它们的属性分配给变量并打印到终端 然后将变量(恰好包含上次赋值的结果)打印到文件中。所以你在这里有一个帖子 如果您想要多个,也需要迭代