1.如何将item 数据model保存到数据库中
- 首先在本地创建好MySQL数据库,再数据库中创建好数据表
# 创建数据库
create database item_database;
set global validate_password_length = 1;
set global validate_password_policy = 0;
grant all on item_database.* to 'xkd'@'%' identified by '123456';
flush privileges;
# 根据item创建数据表
create table item (title varchar(255) not null, image_url varchar(255) not null, date date not null, image_path varchar(255) not null, url varchar(255) not null, url_id char(50) not null primary key);
2. 安装Python MySQL驱动
pip install mysqlclient
3. 在settings文件中修改pipeline
- 然后爬取页面,进行页面解析,返回item交由settings.py文件中定义好的pipelines处理
ITEM_PIPELINES = {
# 'XKD_Dribbble_Spider.pipelines.XkdDribbbleSpiderPipeline': 300,
# 当items.py模块yield之后,默认就是下载image_url的页面
'XKD_Dribbble_Spider.pipelines.ImagePipeline': 1,
'XKD_Dribbble_Spider.pipelines.MysqlPipeline': 2,
}
4. 新建pipeline,写入item到MySQL中
- 接着在pipelines.py文件中新建一个新的pipelines类,如MysqlPipeline,在这个类中初始化数据库连接,重写
process_item()
方法将item的字段读取出来,再提交到数据中表中; 最后运行项目成功后,可以使用命令行工具查看数据是否插入成功;
class MysqlPipeline:
def __init__(self):
self.conn = MySQLdb.connect(host='localhost', user='xkd', password='123456', database='item_database', use_unicode=True, charset='utf8')
self.cursor = self.conn.cursor()
def process_item(self, item, spider):
sql = 'insert into item(title, image_url, date, image_path, url, url_id)' \
'values (%s, %s, %s, %s, %s, %s)'
date = item['date']
self.cursor.execute(sql, args=(item['title'], item['image_url'], date.strftime('%y-%m-%d'), item['image_path'], item['url'], item['url_id']))
self.conn.commit()
return item
def spider_closed(self, spider):
self.cursor.close()
self.conn.close()
5.在本地搭建MySQL数据库的步骤
-
先创建数据库:
create database 数据库名;
-
然后给用户授权:
grant all on 数据库名.* to '用户名'@'%' identified by '密码';
-
记得刷新MySQL的系统权限相关表:
flush privileges;
-
在进入创建好的数据库根据item创建数据库表:
create table item(字段);
6.查看数据库表
-
首先登录MySQL数据库,命令行:
mysql -u用户名 -p密码;
-
然后选择我们创建的数据库,命令行:
use 数据库名;
-
然后就可以查看数据库表是否成功插入数据,命令行:
select * from item;
; -
当数据库表中数据很多的时候,我们可以在查询语句末尾加入一个
\G
参数,横向的表结构会转为使用纵向表结构输出,利于阅读;
网友评论