最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python數(shù)據(jù)持久化存儲實現(xiàn)方法分析

 更新時間:2019年12月21日 12:37:28   作者:為挽月明  
這篇文章主要介紹了Python數(shù)據(jù)持久化存儲實現(xiàn)方法,結(jié)合實例形式分析了Python基于pymongo及mysql模塊的數(shù)據(jù)持久化存儲操作相關實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Python數(shù)據(jù)持久化存儲實現(xiàn)方法。分享給大家供大家參考,具體如下:

1、pymongo的使用

前三步為創(chuàng)建對象

  • 第一步創(chuàng)建連接對象
conn = pymongo.MongoClient('IP地址',27017)
  • 第二步創(chuàng)建庫
db = conn['庫名']
  • 第三步創(chuàng)建表
myset = db['集合名']

  • 第四步把數(shù)據(jù)插入數(shù)據(jù)庫
myset.inset.one({})

#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time : 2019/6/26 8:56
# @Author : #####
# @Site :
# @File : 貓眼電影_mongo存儲.py
# @Software: PyCharm
from urllib import request
import re
import time
import pymongo
class MaoyanSpider(object):
  def __init__(self):
    self.headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5221.400 QQBrowser/10.0.1125.400'}
    #用來計數(shù)
    self.page=1
    #連接對象
    self.coon =pymongo.MongoClient('locslhost',27017)
    #創(chuàng)建庫對象
    self.db=self.coon['maoyaodb']
    #集合對象
    self.myset=self.db['top100']
  def get_page(self,url):
    req = request.Request(url,headers=self.headers)
    res = request.urlopen(req)
    html = res.read().decode('utf-8')
    self.parse_page(html)
  def parse_page(self,html):
    p = re.compile( '<div class="movie-item-info">.*?title="(.*?)".*?class="star">(.*?)</p>.*?class="releasetime">(.*?)</p>',re.S)
    r_list = p.findall(html)
    self.write_mongo(r_list)
  def write_mongo(self,r_list):
    for r_t in r_list:
      d={
        '電影名稱:':r_t[0].strip(),
        '電影主演:':r_t[1].strip(),
        '上映時間:':r_t[2].strip()
      }
    #插入數(shù)據(jù)庫
      self.myset.inset.one(d)
  def work_on(self):
    for pn in range(0,41,10):
      url = 'https://maoyan.com/board/4?offset=%s' % str(pn)
      self.get_page(url)
      print('第%d頁爬取成功' % self.page)
      self.page += 1
      time.sleep(4)
if __name__ == '__main__':
  begin = time.time()
  spider = MaoyanSpider()
  spider.work_on()
  end = time.time()
  print("執(zhí)行時間%.2f" % (end - begin)) #注不完美,仍然需修改

2、mysql的使用

Mysql-front可視化工具,建庫建表添加字段

1、創(chuàng)建連接對象:db = pymysql.connet

2、創(chuàng)建游標對象:cursor = db.sursor

3、執(zhí)行命令:cursor.execute()

4、提交到數(shù)據(jù)庫執(zhí)行

5、關閉:cursor.close

mysql-Front使用流程

1、創(chuàng)建數(shù)據(jù)庫:

localhost--數(shù)據(jù)庫--新建---數(shù)據(jù)庫

數(shù)據(jù)庫名改為maoyan (項目mysql庫名)--- 字符集utf8 ---確定

2、創(chuàng)建表:

流程:選中maoyao數(shù)據(jù)庫 --選中數(shù)據(jù) ----新建 ----出現(xiàn)添加菜單 ---名稱改為top100 ---創(chuàng)建成功

3、往表格中添加字段:

流程:選中top100表單  --- 數(shù)據(jù)庫  ----新建  ----字段  ---出現(xiàn)添加界面 ----名稱改為name  ---默認varchar  ---- 長度50  --確定

用同樣的方法穿件字段star和time

ID一般設置為int 長度視情況而定

更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

相關文章

最新評論

临城县| 谷城县| 武宣县| 通山县| 大丰市| 鲜城| 天祝| 吉安县| 区。| 高尔夫| 东安县| 滨海县| 灵寿县| 许昌县| 依安县| 个旧市| 景洪市| 旅游| 保康县| 乌鲁木齐市| 平安县| 新建县| 昆明市| 凭祥市| 沧州市| 宁城县| 罗山县| 同仁县| 敦煌市| 府谷县| 武鸣县| 莱阳市| 邛崃市| 申扎县| 车致| 阿克苏市| 财经| 韶关市| 清新县| 大邑县| 莱阳市|