爬虫数据持久化的几种常用方法

1、txt

用普通的磁盘IO操作即可

2、csv

1 import csv
2 with open('xxx.csv','w') as f:
3   writer = csv.writer(f)
4   writer.writerow([])
5   writer.writerows([(),(),()])

需注意单条数据写入的参数格式是列表,多条数据写入的参数格式是列表嵌套元组,推荐使用多条数据一次性写入,效率高。

3、json:使用json模块中的dump函数

1 import json
2 data = {'xxx':'yyy'}
3 with open('zzz.json','w') as f:
4     json.dump(ob_data,f,ensure_ascii=False)

4、数据库:MySQL、MongoDB、Redis

存入MySQL:

 1 import pymysql
 2 # __init__(self):
 3 self.db = pymysql.connect('IP',... ...)
 4 self.cursor = self.db.cursor()
 5 # write_data(self):
 6 self.cursor.execute('sql',[data1])
 7 self.cursor.executemany('sql',[(data1),(data2),(data3)])
 8 self.db.commit()
 9 # main(self):
10 self.cursor.close()
11 self.db.close()

存入MongoDB:

import pymongo
# __init__(self):
self.conn = pymongo.MongoClient('IP',27017)
self.db = self.conn['db_name']
self.myset = self.db['set_name']
# write_data(self):
self.myset.insert_one(dict)
# MongoDB - Commmand
>show dbs
>use db_name
>show collections
>db.collection_name.find().pretty()
>db.collection_name.count()
>db.collection_name.drop()
>db.dropDatabase()

存入Redis:

  使用Redis中的字符串、列表、哈希、集合、有序集合进行存储

posted @ 2019-08-18 13:58  Data_worker  阅读(560)  评论(0编辑  收藏  举报