def store_novel_base_infos(self, books: list, db_name: str): db = self._connect_(db_name) bookTable = db.BookBaseInfo for book in books: try: condition = {'bookId': Helper.md5_hash(book.name, False).MD5} record = bookTable.find_one(condition) if(record is None): bookTable.insert_one(book.__dict__) # 转换为 dict self.logger.info("Insert Success:" + book.name) else: self.logger.warning("Insert Failed: Duplicate id --" +book.name) except DuplicateKeyError as dke: self.logger.error("Insert Failed: DuplicateKeyError objectId --\n"+dke) self._close_()
def parsePageBook(self, dom: BeautifulSoup): """ get book(name,link) of per page :param dom: BeautifulSoup Dom :return: Book list """ titleDoms = dom.select('ul.ditu > li') books = [] for titleDom in titleDoms: book = BookBase() book.link = titleDom.select('a')[0]['href'] book.name = titleDom.select('a')[0].text md5Info = Helper.md5_hash(book.name, False) book.bookId = md5Info.MD5 book.salt = bson.binary.Binary(md5Info.Salt) books.append(book) return list(books)
def store_novel_base_info(self, book: BookBase, db_name: str): db = self._connect_(db_name) bookTable = db.BookBaseInfo condition = {'bookId': Helper.md5_hash(book.name, False).MD5} print(condition) record = bookTable.find_one(condition) print(record) if(record is None): # insert print(book.__dict__) insertRes = bookTable.insert_one(book.__dict__) self.logger.info("Insert Success: "+str(insertRes.inserted_id)) else: # update insertRes = bookTable.update(condition, book.__dict__) self.logger.info("Update Success: "+str(insertRes)) self._close_()