python,scrapy爬虫sql之爬取数据存储到mysql的piplelines.py配置

# -*- 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 MySQLdb

DBKWARGS={'db':'test','user':'root', 'passwd':'',
    'host':'localhost','use_unicode':True, 'charset':'utf8'}

class TutorialPipeline(object):

    def __init__(self):
        try:
            self.con = MySQLdb.connect(**DBKWARGS)
        except Exception,e:
            print "Connect db error:",e
        
    def process_item(self, item, spider):
        cur = self.con.cursor()
        sql = "insert into dmoz_book values(%s,%s,%s)"   ###数据库名
        lis = (''.join(item["title"]),''.join(item["link"]), ''.join(item["desc"]))   ###提取内容标题、链接、描述
        try:
            cur.execute(sql,lis)
        except Exception,e:
            print "Insert error:",e
            self.con.rollback()
        else:
            self.con.commit()
        cur.close()
        return item

    def __del__(self):
        try:
            self.con.close()
        except Exception,e:
            print "Close db error",e

当然也可以简写:

import MySQLdb

#这段代码可以写在settings.py文件中
# database connection parameters
#DBKWARGS={'db':'ippool','user':'root', 'passwd':'toor','host':'localhost','use_unicode':True, 'charset':'utf8'}

class CollectipsPipeline(object):

    def process_item(self, item, spider):

        DBKWARGS = spider.settings.get('DBKWARGS')
        con = MySQLdb.connect(**DBKWARGS)
        cur = con.cursor()
        sql = ("insert into proxy(IP,PORT,TYPE,POSITION,SPEED,LAST_CHECK_TIME) "
            "values(%s,%s,%s,%s,%s,%s)")
        lis = (item['IP'],item['PORT'],item['TYPE'],item['POSITION'],item['SPEED'],
            item['LAST_CHECK_TIME'])
        try:
            cur.execute(sql,lis)
        except Exception,e:
            print "Insert error:",e
            con.rollback()
        else:
            con.commit()
        cur.close()
        con.close()
        return item



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值