scrapy数据同步异步存入数据库

# 异步写入mysql数据库
from twisted.enterprise import  adbapi
from MySQLdb import cursors

class MysqlTwistedPipeline(object):
    #这个函数会自动调用
    @classmethod
    def from_settings(cls,settings):
        db_params = dict(
            host=settings["MYSQL_HOST"],
            port=settings["MYSQL_PORT"],
            user=settings["MYSQL_USER"],
            passwd=settings["MYSQL_PASSWD"],
            charset=settings["MYSQL_CHARSET"],
            db=settings["MYSQL_DBNAME"],
            use_unicode=True,
            cursorclass=cursors.DictCursor
        )
        dbpool = adbapi.ConnectionPool('MySQLdb',**db_params)

        return cls(dbpool)
    def __init__(self,dbpool):

        self.dbpool = dbpool

    def process_item(self,item,spider):

        query = self.dbpool.runInteraction(self.do_insert,item)
        query.addErrback(self.handle_error,item,spider)

    def handle_error(self,failure,item,spider):

        print failure
    def do_insert(self,cursor,item):
        sql = 'insert into bole_blogs(title,blog_url,img_src,blog_date,tags,like_count,comment_count,bookmark_count,img_path)VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)'

        cursor.execute(sql, (item["title"], item["blog_url"], item["img_src"][0], item["blog_date"], item["tags"], item["like_count"],item["comment_count"], item["bookmark_count"], item["img_path"]))

from twisted.enterprise import adbapi
from MySQLdb import cursors
class MysqlTwistedSavePipeline(object):
    @classmethod
    def from_settings(cls,settings):
        db_params = dict(
            host = settings["MYSQL_HOST"],
            db = settings["MYSQL_DBNAME"],
            port = settings["MYSQL_PORT"],
            user = settings["MYSQL_USER"],
            passwd = settings["MYSQL_PASSWD"],
            charset = settings["MYSQL_CHARSET"],
            use_unicode = True,
            cursorclass = cursors.DictCursor
        )
        dbpool = adbapi.ConnectionPool('MySQLdb',**db_params)

        return cls(dbpool)

    def __init__(self,dbpool):
        self.dbpool = dbpool

    def process_item(self,item,spider):

        query = self.dbpool.runInteraction(self.do_insert,item)
        query.addErrback(self.handle_error,item,spider)

    def handle_error(self,failure,item,spider):

        print failure

    def do_insert(self,cursor,item):

        sql = 'insert into bole_blogs(title,blog_url,img_src,blog_date,tags,like_count,comment_count,bookmark_count,img_path)VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)'
        cursor.execute(sql, (item["title"], item["blog_url"], item["img_src"][0], item["blog_date"], item["tags"], item["like_count"],item["comment_count"], item["bookmark_count"], item["img_path"]))


# 将item写入数据库
# 小数据可以使用同步写入
import MySQLdb
class MysqlPipeine(object):
    def __init__(self):

        self.conn = MySQLdb.connect(
            host = 'localhost',
            # http默认端口号:80
            # https默认端口号443
            # mysql默认端口号3306
            # flask端口:5000
            # django端口:8000
            port = 3306,
            user = 'root',
            passwd = '123456',
            db = 'jobbole',
            use_unicode = True,
            charset = 'utf8'
        )
        self.cursor = self.conn.cursor()
    # 处理item的函数
    def process_item(self,item,spider):

        # 准备sql语句
        sql = 'insert into bole_blogs(title,blog_url,img_src,blog_date,tags,like_count,comment_count,bookmark_count,img_path)VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)'
        self.cursor.execute(sql,(item["title"],item["blog_url"],item["img_src"][0],item["blog_date"],item["tags"],item["like_count"],item["comment_count"],item["bookmark_count"],item["img_path"]))

        self.conn.commit()

    def close_spider(self,spider):
        self.cursor.close()
        self.conn.close()
--------------------- 
作者:dawning_zyh 
来源:CSDN 
原文:https://blog.csdn.net/dawning_zyh/article/details/77951015 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python Scrapy是一种优秀的开源网络爬虫框架,可以用于从网页中爬取数据。借助其强大的功能,我们可以轻松地将爬取到的数据写入数据库。 首先,我们需要创建一个Scrapy项目并配置好爬虫。在项目中,我们可以定义Item类来表示我们需要提取的数据字段。通过编写爬虫规则,我们可以指定要爬取的网页、需要提取的数据字段以及数据的处理方式。 在编写完爬虫规则后,Scrapy会自动将爬取到的数据封装成Item对象。我们可以在爬虫的回调函数中对这些Item对象进行处理,例如将数据写入数据库。 为了将数据写入数据库,我们可以使用Python的数据库操作库,如MySQLdb或者pymysql。首先,我们需要连接到数据库,并创建一个数据库连接对象。然后,我们可以将爬取到的数据逐条插入到数据库中。 插入数据的具体步骤如下: 1. 导入数据库操作库 2. 连接到数据库 3. 创建游标对象 4. 遍历爬取到的数据 5. 构造插入语句 6. 执行插入操作 7. 提交事务 8. 关闭游标和数据库连接 通过以上步骤,我们可以将爬取到的数据成功写入数据库。 值得注意的是,在爬取大量数据时,为了提高性能和效率,我们可以使用异步IO库,如aiomysql或aiopg,来实现异步插入操作。 总而言之,Python Scrapy可以轻松实现数据的网页爬取,并通过数据库操作库将数据写入数据库。这样,我们可以方便地对爬取到的数据进行存储和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值