class InputMysqlItem(scrapy.Item):
tags = scrapy.Field()
content = scrapy.Field()
编写创建数据库
创建数据库: CREATE DATABASE db DEFAULT CHARACTER SET utf8
创建需要的表:CREATE TABLE crawl_my( id INTEGER NOT NULL tags VARCHAR(64) NULL, content VARCHAR(255) NULL) ENGINE = InnoDB;
编写pipline
import pymysql.cursors
class MySQLPipeline(object):
def __init__(self):
#链接数据库
self.connect = pymysql.connect(
host = '127.0.0.1',#数据库地址
port = 3306,#数据库端口
db = 'db',#数据库名称
user = 'root',#数据库用户名
passwd = 'root',#数据库密码
charset = 'utf8',#数据库编码
use_unicode = True
)
#拿到操作数据库的游标
self.cursor = self.connect.cursor()
def process_item(self,item,spider):
self.cursor.execute(
'''
insert into crawl_my(tags,content)
VALUE (%s,%s)
''',(item['tags'],item['content'])
)
#提交sql
self.connect.commit()
return item