简体   繁体   English

Python字符串比较无法正常工作

[英]Python string comparison not working properly

This is my script. 这是我的剧本。

import MySQLdb import feedparser import string

def checkunique(t):
    #t1 = ''.join(filter(lambda c: c in string.printable, t))
    cur.execute("SELECT title from linkstwo")
    titles = cur.fetchall()
    for k in titles:
        #k1 = ''.join(filter(lambda c: c in string.printable, k))
        print "'%s'" % k
        if t == k:
            return False
    return True

db = MySQLdb.connect ("localhost","root",password,"torrents") print "DB connection successful" cur  = db.cursor()

url = "https://extratorrent.cc/rss.xml?type=popular&cid=4"

feed = feedparser.parse(url)

print "Parsing successful"


for post in feed.entries:
    t = post.title
    m = post.magneturi
    #print "'%s'" % t
    if checkunique(t):
       cur.execute("INSERT INTO linkstwo (title, maglink) VALUES ('%s', '%s')" % \
                    (t, m))
    db.commit()

print "Script ended"

It parses an RSS feed and adds any new entries to a database. 它解析RSS提要,并将所有新条目添加到数据库中。

My problem is the the function checkunique always returns true and I keep getting duplicate entries. 我的问题是checkunique函数始终返回true,并且我不断获取重复的条目。 I tried some of the solutions to remove any non-printable characters that might have found its way in, still no luck. 我尝试了一些解决方案,以删除可能会碰到的所有不可打印字符,但仍然没有运气。

It dosen't make sense to query all table every time you execute the checkunique function. 每次执行checkunique函数时查询所有表checkunique

I would go with some other approach, you could update your sql query in order to check if the title is already exist. 我会采用其他方法,您可以更新sql查询以检查标题是否已经存在。

For example: 例如:

cur.execute("IF (NOT EXISTS(SELECT title FROM linkstwo WHERE title = '%s' ))
    INSERT INTO linkstwo (title, maglink) VALUES ('%s', '%s')" \
                    (t, t, m)))

After editing my script to this, it started functioning as expected. 在为此编辑我的脚本后,它开始按预期运行。

for k in titles:
        #k1 = ''.join(filter(lambda c: c in string.printable, k))
        print "'%s'" % k
        if t == k[0]:
            return False
    return True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM