python使用mongodb数据库
from pymongo import MongoClient,collection class KSpdier(Thread): SEENS = set() def __init__(self, queue: Queue,table:collection.Collection): super().__init__() self.queue = queue self.table = table def save_to_db(self,url:str,title:str,article:str): document = { "title" : title, "content":article, "url":url } try: istid = self.table.insert_one(document).inserted_id print(istid) except Exception as e: print(f'插入数据库失败:{e}') if __name__ == '__main__': url_queue = Queue() url_queue.put("http://www.anchai.com/article/20210426/418576.shtml") client = MongoClient() db = client['anchai'] collection = db['article'] for i in range(1): kc = KancaiSpdier(url_queue,collection) kc.daemon = True kc.start() url_queue.join() print('抓取完成')
python 使用MongoDB插入数据库整个过程