def record_book(bookList, context, isSendMail): mongoCon = pymongo.connection(host='127.0.0.1',port=27017) db = mongoCon.mydatabase for bookItem in bookList: bookInfo = db.book.find_one({'href':bookItem.href}) if not bookInfo: b = { 'bookname': bookItem.bookname.decode('gbk').encode('utf8'), 'href': bookItem.href, 'date': bookItem.date } db.book.insert(b, safe=True) isSendMail = True context = context + bookItem.bookname.decode('gbk').encode('utf8')+',' return context, isSendMail
def init_database(self): conn = pymongo.connection('localhost', 27017)
def __init__(self, host, port): try: self.conn = pymongo.connection(host, port) except: print 'connect to %s:%s failed' % (host, port) exit(0)
import pymongo if __name__ == '__main__': conn = pymongo.connection('localhost',27017)