溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

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

發(fā)布時(shí)間:2020-09-25 09:21:04 來(lái)源:腳本之家 閱讀:133 作者:為挽月明 欄目:開(kāi)發(fā)技術(shù)

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

1、pymongo的使用

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

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

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

#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time : 2019/6/26 8:56
# @Author : #####
# @Site :
# @File : 貓眼電影_mongo存儲(chǔ).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'}
    #用來(lái)計(jì)數(shù)
    self.page=1
    #連接對(duì)象
    self.coon =pymongo.MongoClient('locslhost',27017)
    #創(chuàng)建庫(kù)對(duì)象
    self.db=self.coon['maoyaodb']
    #集合對(duì)象
    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={
        '電影名稱(chēng):':r_t[0].strip(),
        '電影主演:':r_t[1].strip(),
        '上映時(shí)間:':r_t[2].strip()
      }
    #插入數(shù)據(jù)庫(kù)
      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頁(yè)爬取成功' % self.page)
      self.page += 1
      time.sleep(4)
if __name__ == '__main__':
  begin = time.time()
  spider = MaoyanSpider()
  spider.work_on()
  end = time.time()
  print("執(zhí)行時(shí)間%.2f" % (end - begin)) #注不完美,仍然需修改

2、mysql的使用

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

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

2、創(chuàng)建游標(biāo)對(duì)象:cursor = db.sursor

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

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

5、關(guān)閉:cursor.close

mysql-Front使用流程

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

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

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

2、創(chuàng)建表:

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

3、往表格中添加字段:

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

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

ID一般設(shè)置為int 長(zhǎng)度視情況而定

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

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI