我的维基百科代码无法正常工作
My Wikipedia code doesn't work properly
我完全是 Python 的菜鸟,出于某些(可能很明显的)原因,我的代码无法按我想要的方式运行。这是:
import feedparser
import time
feed = feedparser.parse('https://en.wikipedia.org/w/index.php?title=Special:NewPages&feed=rss')
latest = feed.entries[0].title
current = latest
print(current)
while True:
latest = feed.entries[0].title
if current != latest:
current = latest
print(current)
time.sleep(5)
它的目的是在每次维基百科 RSS 提要更新时打印出来。但是,它只会打印出最新一次(可以在 while 语句之前的 print 语句中推测)。这只是逻辑错误吗?
您只在脚本开头请求一次提要。您永远不会在 while 循环中更新它,因此条件永远不会为真。
相反,您应该将请求和解析提要的行移动到循环本身中。
只需在 while 循环中添加这一行。
feed.update
我完全是 Python 的菜鸟,出于某些(可能很明显的)原因,我的代码无法按我想要的方式运行。这是:
import feedparser
import time
feed = feedparser.parse('https://en.wikipedia.org/w/index.php?title=Special:NewPages&feed=rss')
latest = feed.entries[0].title
current = latest
print(current)
while True:
latest = feed.entries[0].title
if current != latest:
current = latest
print(current)
time.sleep(5)
它的目的是在每次维基百科 RSS 提要更新时打印出来。但是,它只会打印出最新一次(可以在 while 语句之前的 print 语句中推测)。这只是逻辑错误吗?
您只在脚本开头请求一次提要。您永远不会在 while 循环中更新它,因此条件永远不会为真。
相反,您应该将请求和解析提要的行移动到循环本身中。
只需在 while 循环中添加这一行。
feed.update