概述
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql,datetime
class XwProjectPipeline(object):
def process_item(self, item, spider):
return item
class MysqlPipeline(object):
def __init__(self,host,user,password,database,port):
self.host = host
self.user = user
self.password = password
self.database = database
self.port = port
@classmethod
def from_crawler(cls,crawler):
return cls(
host = crawler.settings.get("MYSQL_HOST"),
user = crawler.settings.get("MYSQL_USER"),
password = crawler.settings.get("MYSQL_PASS"),
database = crawler.settings.get("MYSQL_DATABASE"),
port = crawler.settings.get("MYSQL_PORT"),
)
def open_spider(self, spider):
'''负责连接数据库'''
self.db = pymysql.connect(host=self.host,user=self.user,password=self.password,database=self.database,charset="utf8",port=self.port)
self.cursor = self.db.cursor()
def process_item(self, item, spider):
'''执行数据表的写入操作'''
#组装sql语句
data = dict(item)
data['snapshot_time'] = datetime.datetime.utcnow()
keys = ','.join(data.keys())
values=','.join(['%s']*len(data))
sql = "insert ignore into search_product_list(%s) values(%s)"%(keys,values)
print('sqlsqlsql')
print(sql)
#指定参数,并执行sql添加
self.cursor.execute(sql,tuple(data.values()))
#事务提交
self.db.commit()
return item
def close_spider(self, spider):
'''关闭连接数据库'''
self.db.close()
最后
以上就是含糊白羊为你收集整理的mysql(scrapy)的全部内容,希望文章能够帮你解决mysql(scrapy)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复