import time, config, techlab c = config.cursor() def fix_quotes(i): return ['\\"'.join(x.split("\"")) for x in i] while True: c.execute("SELECT * FROM stockList") stocks = c.fetchall() for stock in stocks: try: news = techlab.extended_news(stock['stock'],stock['name']) for item in news: item = fix_quotes(item) c.execute("SELECT * FROM mining_2 WHERE subject=\"%s\""%(item[0])) if len(c.fetchall()) is 0: print item c.execute("INSERT INTO mining_2 SET `time`=NOW(), subject=\"%s\",news=\"%s\", symbol=\"%s\""%(item[0],item[1],stock['stock'])) except:pass