我正在尝试重新格式化一个RSS文件,其中长而复杂的条目被添加到文件的开头。我是个笨蛋,不知道从哪里开始,所以我在这个网站上寻找解决方案,但还没有找到。有些命令对我来说很陌生,但我已经处理了很多文件,并按计划下载了一个提要。你知道吗
我试图在RSS提要中找到第四个条目(由“/item”标记表示),但是,在这里我遇到了一个无法找到答案的障碍。你知道吗
(Python3)
import time
import sched
import urllib.request
import shutil
scheduler = sched.scheduler(time.time, time.sleep)
def rss():
# Download the file from `url` and save it locally under `file_name`:
with urllib.request.urlopen('http://any.website.here/rss') as response, open('test.xml', 'wb') as out_file:
shutil.copyfileobj(response, out_file)
print('Updating RSS')
def trunc():
a = ()
a = open('test.xml', 'r+', encoding = 'utf-8')
c = (0)
for line in a:
if a.readline() == '</item>':
c = c+1
print(c, 'items found!' at )
if c == 4:
return a.tell()
a.seek(0), print(a.read())
a.close
def scheduler_rss():
scheduler.enter(0, 1, rss, ()) # calls rss
scheduler.run()
trunc()
#time.sleep(43200) #time in seconds, this is 12 hours
time.sleep(30) #Variable for testing
for i in range(100):
scheduler_rss()
这只是寻找解决方案的许多失败尝试的最近一次迭代。你知道吗
不管怎样,这是我一直在努力的RSS。。。http://nightvale.libsyn.com/rss它会按照我的指示将文件复制到我的硬盘上,然后RSS提要阅读器(在我的例子中是一个ticker)就可以读取该文件。基本上,我想我是在问:我如何在文件中找到从那一点开始截断文件的位置,那一点是在.xml文件中调用标记的第四次,记住提要将定期更新,并且每个版本的标记将不在同一位置?你知道吗
如果您对另一种方法感兴趣,下面介绍如何使用python的
xml.dom
模块实现这一点。你也可以用xml.etree
来做这个。你知道吗为每个
<item>
标记打印这样的内容,直到第4个:现在,要截断第4个元素以外的所有项:
相关问题 更多 >
编程相关推荐