自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教你用Python玩轉(zhuǎn)MySQL

開發(fā) 后端
爬蟲采集下來的數(shù)據(jù)除了存儲(chǔ)在文本文件、excel之外,還可以存儲(chǔ)在數(shù)據(jù)集,如:Mysql,redis,mongodb等,今天辰哥就來教大家如何使用Python連接Mysql,并結(jié)合爬蟲為大家講解。

 [[400168]]

大家好,我是辰哥。

爬蟲采集下來的數(shù)據(jù)除了存儲(chǔ)在文本文件、excel之外,還可以存儲(chǔ)在數(shù)據(jù)集,如:Mysql,redis,mongodb等,今天辰哥就來教大家如何使用Python連接Mysql,并結(jié)合爬蟲為大家講解。

前提:這里默認(rèn)大家已經(jīng)安裝好mysql。

01Mysql簡介

mysql是關(guān)系型數(shù)據(jù)庫,支持大型的數(shù)據(jù)庫,可以處理擁有上千萬條記錄的大型數(shù)據(jù)庫。通過爬蟲采集的數(shù)據(jù)集存儲(chǔ)到mysql后,可以借助mysql的關(guān)聯(lián)查詢將相關(guān)的數(shù)據(jù)一步取出。具體的作用這里就不贅述了,下面開始進(jìn)入實(shí)際操作。

1.安裝pymysql

通過下面這個(gè)命令進(jìn)行安裝

  1. pip install pymysql 

pymysql庫:Python3鏈接mysql

備注:

ps:MYSQLdb只適用于python2.x

python3不支持MYSQLdb,取而代之的是pymysql

運(yùn)行會(huì)報(bào):ImportError:No module named 'MYSQLdb'

2.python連接mysql

  1. import pymysql as pmq 
  2. #connect(ip.user,password,dbname) 
  3. con = pmq.connect('localhost','root','123456','python_chenge'
  4. #操作游標(biāo) 
  5. cur = con.cursor() 

localhost是本機(jī)ip,這里用localhost表示是當(dāng)前本機(jī),否則將localhost改為對應(yīng)的數(shù)據(jù)庫ip。

root是數(shù)據(jù)庫用戶名,123456是數(shù)據(jù)庫密碼,python_chenge是數(shù)據(jù)庫名。

圖上的數(shù)據(jù)庫python_chenge已經(jīng)建立好(建好之后,才能用上面代碼去連接),建好之后,當(dāng)前是沒有表的,現(xiàn)在開始用Python進(jìn)行建表,插入、查詢,修改,刪除等操作(結(jié)合爬蟲去講解)

02建表

在存儲(chǔ)之前,先通過python創(chuàng)建表,字段有四個(gè)(一個(gè)主鍵+電影名稱,鏈接,評分)

  1. # 創(chuàng)建 movie 表 
  2. movie_sql= ''
  3.         create table movie( 
  4.             id int AUTO_INCREMENT  primary key not null
  5.             title varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci  not null
  6.             url varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci  not null
  7.             rate float  not null 
  8.         ) 
  9. ''
  10. # 執(zhí)行sql語句 
  11. cur.execute(movie_sql) 
  12. # 提交到數(shù)據(jù)庫執(zhí)行 
  13. con.commit() 

創(chuàng)建表movie,字段分別為(id ,title ,url ,rate ),CHARACTER SET utf8 COLLATE utf8_general_ci是字符串編碼設(shè)置為utf8格式

id是主鍵primary key,int類型,AUTO_INCREMENT自增,非空not null

title,url 是字符串類型varchar(100),同樣非空

評分rate 是帶小數(shù)的數(shù)字,所以是float,同樣非空

03插入數(shù)據(jù)

爬蟲已經(jīng)采集到數(shù)據(jù),python已經(jīng)建好表,接著可以將采集的數(shù)據(jù)插入到數(shù)據(jù)庫,這里介紹兩種方式

  1. ### 插入數(shù)據(jù) 
  2. def insert(title,url,rate): 
  3.     # 插入數(shù)據(jù)一 
  4.     #cur.execute("INSERT INTO movie(title,url,rate) VALUES('"+str(title)+"','"+str(url)+"',"+str(rate)+")"
  5.     # 插入數(shù)據(jù)二 
  6.     sql = "INSERT INTO movie(title,url,rate) VALUES('"+str(title)+"','"+str(url)+"',"+str(rate)+")" 
  7.     cur.execute(sql) 
  8.     # 提交到數(shù)據(jù)庫執(zhí)行 
  9.     con.commit()     

id是自增的,所以不需要在傳值進(jìn)去。

定義好插入數(shù)據(jù)庫方法后,開始往數(shù)據(jù)庫進(jìn)行存儲(chǔ)

  1. for i in json_data['subjects']: 
  2.     insert(i['title'],i['url'],i['rate']) 

04查詢

1.查詢所有

查詢表中所有數(shù)據(jù)

  1. # 查詢 
  2. cur.execute('select * from movie'
  3. results = cur.fetchall() 
  4. for row in results: 
  5.     Id = row[0] 
  6.     title = row[1] 
  7.     print("id=%s,title=%s" % (Id, title)) 

2.查詢指定的數(shù)據(jù)

比如查詢標(biāo)題為:唐人街3這一條數(shù)據(jù)的所有字段

  1. #查詢單條 
  2. cur.execute('select * from movie where title="唐人街探案3"'
  3. results = cur.fetchall() 
  4. for row in results: 
  5.     Id = row[0] 
  6.     title = row[1] 
  7.     url = row[2] 
  8.     rate = row[3] 
  9.     print("id=%s,title=%s,url=%s,rate=%s" % (Id, title,url,rate)) 

05更新修改

更新數(shù)據(jù),還是以上面:唐人街3為例,id為7,將唐人街3評分從5.5改為6

  1. ### 更新 
  2. def update(): 
  3.     sql = "update movie set rate='6' where Id = {0}".format(7) 
  4.     cur.execute(sql) 
  5.     con.commit() 

同時(shí)看一下數(shù)據(jù)庫

06刪除

同樣還是以唐人街為例,其id為7,刪除的話咱們可以更新id去刪除

  1. def delete(Id): 
  2.     sql = "delete from movie where Id = {0}".format(Id) 
  3.     cur.execute(sql) 
  4.     con.commit() 

刪除之后,就沒有第7條數(shù)據(jù)了,說明刪除成功

07小結(jié)

今天的技術(shù)講解文章就到此結(jié)束,主要是將了如何通過python去連接mysql,并進(jìn)行建表,插入數(shù)據(jù),查詢,更新修改和刪除。

本文轉(zhuǎn)載自微信公眾號「Python研究者」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系Python研究者公眾號。

責(zé)任編輯:武曉燕 來源: Python研究者
相關(guān)推薦

2015-03-23 12:33:28

2019-01-24 09:00:00

PythonAutoML機(jī)器學(xué)習(xí)

2015-04-22 11:29:45

PythonPython創(chuàng)建瀑布圖

2023-08-03 08:51:07

2020-04-09 09:52:42

Python數(shù)據(jù)技術(shù)

2021-08-09 13:31:25

PythonExcel代碼

2021-12-26 18:32:26

Python Heic 文件

2014-07-22 10:19:19

NeoBundle

2017-07-19 10:22:07

2022-02-18 10:34:19

邏輯回歸KNN預(yù)測

2023-10-27 11:38:09

PythonWord

2019-09-05 10:07:23

ZAODeepfakes換臉

2021-05-10 06:48:11

Python騰訊招聘

2013-08-23 09:37:32

PythonPython游戲Python教程

2018-05-14 10:43:53

平衡數(shù)據(jù)數(shù)據(jù)分析Python

2021-02-02 13:31:35

Pycharm系統(tǒng)技巧Python

2021-12-11 20:20:19

Python算法線性

2020-03-25 14:40:45

語言編程語言Hello

2014-07-21 09:51:10

AndroidResflux修改應(yīng)用

2018-01-02 16:48:58

Python 微信安卓
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號