scrapy-pipeline的方法

a

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
# from pymongo import MongoClient

# client = MongoClient()
# collection = client['SpiderAnything']['hr']
import pymysql


class SpiderSuningBookPipeline(object):
    def process_item(self, item, spider):
        if spider.name == 'book_info':
            # collection.insert(dict(item))
            sql = """
                insert into book(title,author,download_text,new) values('%s','%s','%s','%s')"""\
                  %(
                item['title'],
                item['author'],
                item['download_text'],
                item['new']
    )
            print(sql)
            self.cursor.execute(sql)
        elif spider.name == 'dangdang':
            print(item)

        return item

    def open_spider(self, spider):
        # 连接数据库
        self.connect = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        db='study',
        user='root',
        passwd='123456',
        charset='utf8',
        use_unicode=True)

        # 通过cursor执行增删查改
        self.cursor = self.connect.cursor()
        self.connect.autocommit(True)

    def close_spider(self, spider):
        self.cursor.close()
        self.connect.close()

 

posted on 2019-04-11 17:33 .Tang 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/tangpg/p/10691114.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值