0

我正在创建一个简单的 python 脚本,它使用 xmlrpc API 检查 WordPress 博客上的新评论。

我陷入了一个循环,该循环应该告诉我是否有新评论。这是代码:

def checkComm():
    old_commCount = 0;
    server = xmlrpclib.ServerProxy(server_uri); # connect to WP server
    comments = server.wp.getComments(blog_id, server_admin, admin_pass, filters);
    new_commCount = len(comments);
    if new_commCount > old_commCount:
        print "there are new comments"
        old_commCount = new_commCount
    else:
        print "no new comments"

while True:
    checkComm()
    time.sleep(60)

我跳过了 blog_id、server_admin 等变量,因为它们对这个问题没有任何帮助。

你能告诉我的代码有什么问题吗?

提前非常感谢。

4

1 回答 1

0

您想将它作为参数传递,因为每次调用函数时都会重置它:

def checkComm(old_commCount): # passed as a parameter
    server = xmlrpclib.ServerProxy(server_uri) # connect to WP server
    comments = server.wp.getComments(blog_id, server_admin, admin_pass, filters)
    new_commCount = len(comments)
    if new_commCount > old_commCount:
        print "there are new comments"
        old_commCount = new_commCount
        return old_commCount # return it so you can update it
    else:
        print "no new comments"
        return old_commCount

comm_count = 0 # initialize it here
while True:
    comm_count = checkComm(comm_count) # update it every time
    time.sleep(60)
于 2013-04-26T12:19:32.747 回答