当前位置:首页 > 日记本 > 正文内容

python使用mongodb数据库

zhangchap3年前 (2021-05-18)日记本440
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插入数据库整个过程

标签: python笔记
分享给朋友:

相关文章

python url.parse模块编码解码

from urllib.parse import quote,unquote,urlencode # 对汉字进行编码使用 quote ...

python jieba分词自定义分词器及自定义词典

import jieba jieba.initialize() # 自定义分词器的写法 n_c = jieba.Tokenizer(dictionary=...

python正则替换替换文本特殊字符和html代码

import re h2 = '「新裤子/痛仰/陈粒/达达」2021贵阳草莓音乐节全阵容公布!' h2 = re.sub(r...

python 字典排序

prefix_word = {'怎么': 4, '昨晚': 27, '会': 56...

python chardet模块自动识别编码

import chardet str = b'Hello word' str1 = '你好,世界。'.e...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。