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

python使用mongodb数据库

zhangchap3年前 (2021-05-18)日记本436
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 函数 开启多线程示例

from threading import Thread def readfile(queue:Queue):    &nbs...

python补全网址代码示例

from urllib.parse import urljoin absurl = urljoin(backend,url) #backend:根...

python jieba分词

import jieba from jieba.analyse import tfidf words = jieba.lcut('...

python fake_useragent 模块用法

我们每次发送requests请求时通过random从中随机获取一个随机UserAgent,两行代码即可完成UserAgent的不停更换 from fake_useragent i...

python列表排序(以字符串长度)

M = ['a', 'sss', 'bb'] 第一种: m = M.sort(key&n...

python函数开启多线程

from threading import Thread 以下是代码举例: def main(num):    &nbs...

发表评论

访客

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