无论出于何种原因,当我检查列表中某个项目的成员资格时,都表示该列表中没有任何项目。但是,每当我打印项目时,它都会显示所有上述项目。我已经尝试过使用列表和集合。我究竟做错了什么?

import feedparser
rss = feedparser.parse("https://website.com/feed")
new_posts = []
date_time = set()

for entry in rss.entries:
    if entry not in new_posts:
        new_posts.append(entry.published)
        print("added")


for entry in rss.entries:
    if entry in new_posts:
        print("yes")
    else:
        print("no")


输出:

added
added
added
added
added
added
added
added
added
added
added
added
no
no
no
no
no
no
no
no
no
no
no
no

最佳答案

entry.published不在new_posts中时,您要将entry附加到new_posts。您应该将entry本身附加到new_posts上。

更改:

new_posts.append(entry.published)


至:

new_posts.append(entry)

关于python - 使用feedparser从博客中获取每个唯一项。检查for循环中的列表成员身份不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57191726/

10-12 22:03
查看更多