python3下的数据存储 Pymysql(mysql) Pymongo(mongo)

安装pymysql

在Python中使用MySQL,有两种方式,使用ORM(对象关系映射)框架和数据库模块,在此我们使用数据库模块pymysql(Python3)。
安装pymysql

Shell
pip install <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/pymysql" title="View all posts in pymysql" target="_blank">pymysql</a></span>
1
pip install pymysql

在爬虫程序中使用mysql
我们以之前爬取今日头条的例子来扩展;
之前的代码是这样的:

Python
# coding:utf-8 import requests import json url = 'http://www.toutiao.com/api/pc/focus/' wbdata = requests.get(url).text data = json.loads(wbdata) news = data['data']['pc_feed_focus'] for n in news: title = n['title'] img_url = n['image_url'] url = n['media_url'] print(url,title,img_url)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# coding:utf-8
import requests
import json
 
url = 'http://www.toutiao.com/api/pc/focus/'
wbdata = requests . get ( url ) . text
 
data = json . loads ( wbdata )
news = data [ 'data' ] [ 'pc_feed_focus' ]
 
for n in news :
   title = n [ 'title' ]
   img_url = n [ 'image_url' ]
   url = n [ 'media_url' ]
   print ( url , title , img_url )

在最后,我们直接使用print将数据打印了出来。
现在我们使用pymysql将数据存储到Mysql中。
(创建数据库toutiao,创建数据表data)
修改的代码如下:

Python
# coding:utf-8import requests import json import pymysql conn = pymysql.connect(host='localhost',port=3307,user='root',password='usbw',db='toutiao',charset='utf8') cursor = conn.cursor() url = 'http://www.toutiao.com/api/pc/focus/'wbdata = requests.get(url).text data = json.loads(wbdata) news = data['data']['pc_feed_focus']for n in news: title = n['title'] img_url = n['image_url'] url = n['media_url'] print(url,title,img_url) cursor.execute("INSERT INTO data(title,img_url,url)VALUES('{0}','{1}','{2}');".format(title,img_url,url)) conn.commit() cursor.close() conn.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# coding:utf-8import requests
import json
import pymysql
 
conn = pymysql . connect ( host = 'localhost' , port = 3307 , user = 'root' , password = 'usbw' , db = 'toutiao' , charset = 'utf8' )
cursor = conn . cursor ( )
 
url = 'http://www.toutiao.com/api/pc/focus/' wbdata = requests . get ( url ) . text
 
data = json . loads ( wbdata )
news = data [ 'data' ] [ 'pc_feed_focus' ] for n in news :
     title = n [ 'title' ]
     img_url = n [ 'image_url' ]
     url = n [ 'media_url' ]      print ( url , title , img_url )
     cursor . execute ( "INSERT INTO data(title,img_url,url)VALUES('{0}','{1}','{2}');" . format ( title , img_url , url ) )
     conn . commit ( )
 
cursor . close ( )
conn . close ( )
Python
#引入pymysql模块: import pymysql #建立一个mysql的连接: conn = pymysql.connect(host='localhost',port=3307,user='root',password='usbw',db='toutiao',charset='utf8') #创建一个游标cursor: cursor = conn.cursor() #执行一个SQL语句: cursor.execute("INSERT INTO data(title,img_url,url)VALUES('{0}','{1}','{2}');".format(title,img_url,url)) #提交执行(因为对数据进行和修改,如果只是select,则不需要): conn.commit() #最后,关闭连接: cursor.close() conn.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
#引入pymysql模块:
import pymysql
#建立一个mysql的连接:
conn = pymysql . connect ( host = 'localhost' , port = 3307 , user = 'root' , password = 'usbw' , db = 'toutiao' , charset = 'utf8' )
#创建一个游标cursor:
cursor = conn . cursor ( )
#执行一个SQL语句:
cursor . execute ( "INSERT INTO data(title,img_url,url)VALUES('{0}','{1}','{2}');" . format ( title , img_url , url ) )
#提交执行(因为对数据进行和修改,如果只是select,则不需要):
conn . commit ( )
#最后,关闭连接:
cursor . close ( )
conn . close ( )

嗯,将数据保存在MySQL就完成了,更多的MySQL和PyMySQL的用法,还请看文档
下面看看MongoDB

MongoDB储存

1.下载并安装MongoDB:
https://www.mongodb.com/download-center

2.运行mongodb:
进入安装好之后的mongo目录的bin目录,打开命令行窗口,输入“mongod --dbpath=数据存放路径”

3.安装pymongo:

Shell
pip install <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/pymongo" title="View all posts in pymongo" target="_blank">pymongo</a></span>
1
pip install pymongo

4、使用MongoDB和PyMongo
依然是扩展爬取今日头条的例子,先上代码:

Python
# coding:utf-8 import requests import json import pymongo conn = pymongo.MongoClient(host='localhost',port=27017) toutiao = conn['toutiao'] newsdata = toutiao['news'] url = 'http://www.toutiao.com/api/pc/focus/'wbdata = requests.get(url).text data = json.loads(wbdata) news = data['data']['pc_feed_focus']for n in news: title = n['title'] img_url = n['image_url'] url = n['media_url'] data = { 'title':title, 'img_url':img_url, 'url':url } newsdata.insert_one(data)for i in newsdata.find(): print(i)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# coding:utf-8
import requests
import json
import pymongo
 
conn = pymongo . MongoClient ( host = 'localhost' , port = 27017 )
toutiao = conn [ 'toutiao' ]
newsdata = toutiao [ 'news' ]
 
url = 'http://www.toutiao.com/api/pc/focus/' wbdata = requests . get ( url ) . text
 
data = json . loads ( wbdata )
news = data [ 'data' ] [ 'pc_feed_focus' ] for n in news :
     title = n [ 'title' ]
     img_url = n [ 'image_url' ]
     url = n [ 'media_url' ]
     data = {          'title' : title ,          'img_url' : img_url ,          'url' : url
     }
     newsdata . insert_one ( data ) for i in newsdata . find ( ) :
     print ( i )
Python
#Pymongo相关的代码为: #引入模块 import pymongo #连接到Mongo conn = pymongo.MongoClient(host='localhost',port=27017) #选择或创建数据库 toutiao = conn['toutiao'] #选择或创建数据集合 newsdata = toutiao['news'] #插入一行数据: newsdata.insert_one(data) #查询数据 newsdata.find() #如此,简单地对数据进行数据库存储就完成了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#Pymongo相关的代码为:
#引入模块
 
import pymongo
#连接到Mongo
 
conn = pymongo . MongoClient ( host = 'localhost' , port = 27017 )
#选择或创建数据库
 
toutiao = conn [ 'toutiao' ]
#选择或创建数据集合
 
newsdata = toutiao [ 'news' ]
#插入一行数据:
 
newsdata . insert_one ( data )
#查询数据
 
newsdata . find ( )
#如此,简单地对数据进行数据库存储就完成了。

 

Python3 对 redis的操作

redis连接实例是线程安全的,可以直接将redis连接实例设置为一个全局变量,直接使用。如果需要另一个Redis实例(or Redis数据库)时,就需要重新创建redis连接实例来获取一个新的连接。同理,python的redis没有实现select命令。

1.安装redis

Shell
pip install redis
1
pip install redis

2.redis 的增删改查

Shell
import redis r = redis.Redis(host='localhost',port=6379,password="password",db=0) r.set('guo','shuai') #增加 True r.get('guo') #查询 'shuai' r['guo'] #查询 'shuai' >>r.keys() #获取键值 ['guo'] >>r.dbsize() #当前数据库包含多少条数据 1L >>r.delete('guo') #redis 删除 1 >>r.save() #执行“检查点”操作,将数据写回磁盘。保存时阻塞 True >>r.get('guo'); >>r.flushdb() #清空r中的所有数据 True ''' pipeline操作 管道(pipeline)是redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器-客户端之间反复的TCP数据库包,从而大大提高了执行批量命令的功能。 ''' >>p = r.pipeline() --创建一个管道 >>p.set('hello','redis') >>p.sadd('faz','baz') >>p.incr('num') >>p.execute() [True, 1, 1] >>r.get('hello') 'redis' #管道的命令可以写在一起,如: >>p.set('hello','redis').sadd('faz','baz').incr('num').execute() #默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import redis
r = redis .Redis ( host = 'localhost' , port = 6379 , password = "password" , db = 0 )
r .set ( 'guo' , 'shuai' ) #增加
True
r .get ( 'guo' ) #查询
'shuai'
r [ 'guo' ]    #查询
'shuai'
>> r .keys ( ) #获取键值
[ 'guo' ]
>> r .dbsize ( )          #当前数据库包含多少条数据
1L
>> r .delete ( 'guo' ) #redis 删除
1
>> r .save ( )                #执行“检查点”操作,将数据写回磁盘。保存时阻塞
True
>> r .get ( 'guo' ) ;
>> r .flushdb ( )          #清空r中的所有数据
True
'''
pipeline操作
管道( pipeline)是 redis在提供单个请求中缓冲多条服务器命令的基类的子类。它通过减少服务器 -客户端之间反复的 TCP数据库包,从而大大提高了执行批量命令的功能。
'''
>> p = r .pipeline ( )          --创建一个管道
>> p .set ( 'hello' , 'redis' )
>> p .sadd ( 'faz' , 'baz' )
>> p .incr ( 'num' )
>> p .execute ( )
[ True , 1 , 1 ]
>> r .get ( 'hello' )
'redis'
#管道的命令可以写在一起,如:
 
>> p .set ( 'hello' , 'redis' ) .sadd ( 'faz' , 'baz' ) .incr ( 'num' ) .execute ( )
#默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。

 

redis应用场景 – 页面点击数

《Redis Cookbook》对这个经典场景进行详细描述。假定我们对一系列页面需要记录点击次数。例如论坛的每个帖子都要记录点击次数,而点击次数比回帖的次数的多得多。如果使用关系数据库来存储点击,可能存在大量的行级锁争用。所以,点击数的增加使用redis的INCR命令最好不过了。
当redis服务器启动时,可以从关系数据库读入点击数的初始值(1237这个页面被访问了34634次)

Shell
>>r.set("visit:1237:totals",34634) True
1
2
>> r .set ( "visit:1237:totals" , 34634 )
True
Python
#每当有一个页面点击,则使用INCR增加点击数即可。 >>r.incr("visit:1237:totals") 34635 >>r.incr("visit:1237:totals") 34636 #页面载入的时候则可直接获取这个值 >>r.get ("visit:1237:totals") '34636'
1
2
3
4
5
6
7
8
#每当有一个页面点击,则使用INCR增加点击数即可。
>> r . incr ( "visit:1237:totals" )
34635
>> r . incr ( "visit:1237:totals" )
34636
#页面载入的时候则可直接获取这个值
>> r . get ( "visit:1237:totals" )
'34636'

使用hash类型保存多样化对象
当有大量类型文档的对象,文档的内容都不一样时,(即“表”没有固定的列),可以使用hash来表达。

Python
>>r.hset('users:jdoe', 'name', "John Doe") 1L >>r.hset('users:jdoe', 'email', 'John@test.com') 1L >>r.hset('users:jdoe', 'phone', '1555313940') 1L >>r.hincrby('users:jdoe', 'visits', 1) 1L >>r.hgetall('users:jdoe') {'phone': '1555313940', 'name': 'John Doe', 'visits': '1', 'email': 'John@test.com'} >>r.hkeys('users:jdoe') ['name', 'email', 'phone', 'visits']
1
2
3
4
5
6
7
8
9
10
11
12
>> r . hset ( 'users:jdoe' ,    'name' , "John Doe" )
1L
>> r . hset ( 'users:jdoe' , 'email' , 'John@test.com' )
1L
>> r . hset ( 'users:jdoe' ,    'phone' , '1555313940' )
1L
>> r . hincrby ( 'users:jdoe' , 'visits' , 1 )
1L
>> r . hgetall ( 'users:jdoe' )
{ 'phone' : '1555313940' , 'name' : 'John Doe' , 'visits' : '1' , 'email' : 'John@test.com' }
>> r . hkeys ( 'users:jdoe' )
[ 'name' , 'email' , 'phone' , 'visits' ]

应用场景 – 社交圈子数据
在社交网站中,每一个圈子(circle)都有自己的用户群。通过圈子可以找到有共同特征(比如某一体育活动、游戏、电影等爱好者)的人。当一个用户加入一个或几个圈子后,系统可以向这个用户推荐圈子中的人。
我们定义这样两个圈子,并加入一些圈子成员。

Python
>>r.sadd('circle:game:lol','user:debugo') 1 >>r.sadd('circle:game:lol','user:leo') 1 >>r.sadd('circle:game:lol','user:Guo') 1 &amp;gt;&amp;gt; r.sadd('circle:soccer:InterMilan','user:Guo') 1 >>r.sadd('circle:soccer:InterMilan','user:Levis') 1 >>r.sadd('circle:soccer:InterMilan','user:leo')
1
2
3
4
5
6
7
8
9
10
11
>> r . sadd ( 'circle:game:lol' , 'user:debugo' )
1
>> r . sadd ( 'circle:game:lol' , 'user:leo' )
1
>> r . sadd ( 'circle:game:lol' , 'user:Guo' )
1
& amp ; gt ; & amp ; gt ; r . sadd ( 'circle:soccer:InterMilan' , 'user:Guo' )
1
>> r . sadd ( 'circle:soccer:InterMilan' , 'user:Levis' )
1
>> r . sadd ( 'circle:soccer:InterMilan' , 'user:leo' )

#获得某一圈子的成员

Python
>>r.smembers('circle:game:lol') set(['user:Guo', 'user:debugo', 'user:leo']) redis&amp;gt; smembers circle:jdoe:family 可以使用集合运算来得到几个圈子的共同成员: >>r.sinter('circle:game:lol', 'circle:soccer:InterMilan') set(['user:Guo', 'user:leo']) >>r.sunion('circle:game:lol', 'circle:soccer:InterMilan') set(['user:Levis', 'user:Guo', 'user:debugo', 'user:leo'])
1
2
3
4
5
6
7
8
9
>> r . smembers ( 'circle:game:lol' )
set ( [ 'user:Guo' , 'user:debugo' , 'user:leo' ] )
redis & amp ; gt ; smembers circle : jdoe : family
可以使用集合运算来得到几个圈子的共同成员:
 
>> r . sinter ( 'circle:game:lol' , 'circle:soccer:InterMilan' )
set ( [ 'user:Guo' , 'user:leo' ] )
>> r . sunion ( 'circle:game:lol' , 'circle:soccer:InterMilan' )
set ( [ 'user:Levis' , 'user:Guo' , 'user:debugo' , 'user:leo' ] )

应用场景 – 实时用户统计

Counting Online Users with Redis介绍了这个方法。当我们需要在页面上显示当前的在线用户时,就可以使用Redis来完成了。首先获得当前时间(以Unix timestamps方式)除以60,可以基于这个值创建一个key。然后添加用户到这个集合中。当超过你设定的最大的超时时间,则将这个集合设为过期;而当需要查询当前在线用户的时候,则将最后N分钟的集合交集在一起即可。由于redis连接对象是线程安全的,所以可以直接使用一个全局变量来表示。

Python
import time from redis import Redis from datetime import datetime ONLINE_LAST_MINUTES = 5 redis = Redis() def mark_online(user_id): #将一个用户标记为online now = int(time.time()) #当前的UNIX时间戳 expires = now + (app.config['ONLINE_LAST_MINUTES'] * 60) + 10 #过期的UNIX时间戳 all_users_key = 'online-users/%d' % (now // 60) #集合名,包含分钟信息 user_key = 'user-activity/%s' % user_id p = redis.pipeline() p.sadd(all_users_key, user_id) #将用户id插入到包含分钟信息的集合中 p.set(user_key, now) #记录用户的标记时间 p.expireat(all_users_key, expires) #设定集合的过期时间为UNIX的时间戳 p.expireat(user_key, expires) p.execute() def get_user_last_activity(user_id): #获得用户的最后活跃时间 last_active = redis.get('user-activity/%s' % user_id) #如果获取不到,则返回None if last_active is None: return None return datetime.utcfromtimestamp(int(last_active)) def get_online_users(): #获得当前online用户的列表 current = int(time.time()) // 60 minutes = xrange(app.config['ONLINE_LAST_MINUTES']) return redis.sunion(['online-users/%d' % (current - x) #取ONLINE_LAST_MINUTES分钟对应集合的交集 for x in minutes])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import time
from redis import Redis
from datetime import datetime
ONLINE_LAST_MINUTES = 5
redis = Redis ( )
 
def mark_online ( user_id ) :          #将一个用户标记为online
     now = int ( time . time ( ) )          #当前的UNIX时间戳
     expires = now + ( app . config [ 'ONLINE_LAST_MINUTES' ] * 60 ) + 10      #过期的UNIX时间戳
     all_users_key = 'online-users/%d' % ( now / / 60 )          #集合名,包含分钟信息
     user_key = 'user-activity/%s' % user _id
     p = redis . pipeline ( )
     p . sadd ( all_users_key , user_id )                          #将用户id插入到包含分钟信息的集合中
     p . set ( user_key , now )                                    #记录用户的标记时间
     p . expireat ( all_users_key , expires )                      #设定集合的过期时间为UNIX的时间戳
     p . expireat ( user_key , expires )
     p . execute ( )
 
def get_user_last_activity ( user_id ) :          #获得用户的最后活跃时间
     last_active = redis . get ( 'user-activity/%s' % user_id )    #如果获取不到,则返回None
     if last_active is None :
         return None
     return datetime . utcfromtimestamp ( int ( last_active ) )
 
def get_online_users ( ) :                      #获得当前online用户的列表
     current = int ( time . time ( ) ) / / 60
     minutes = xrange ( app . config [ 'ONLINE_LAST_MINUTES' ] )
     return redis . sunion ( [ 'online-users/%d' % ( current - x )          #取ONLINE_LAST_MINUTES分钟对应集合的交集
                         for x in minutes ] )



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值