scrapy MysqlPipeline 同步和异步

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
+关注继续查看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import MySQLdb
import MySQLdb.cursors
 
 
class MysqlPipeline(object):
    #采用同步的机制写入mysql
    def __init__(self):
        self.conn = MySQLdb.connect('192.168.0.106''root''root''article_spider', charset="utf8", use_unicode=True)
        self.cursor = self.conn.cursor()
 
    def process_item(self, item, spider):
        insert_sql = """
            insert into jobbole_article(title, url, create_date, fav_nums)
            VALUES (%s, %s, %s, %s)
        """
        self.cursor.execute(insert_sql, (item["title"], item["url"], item["create_date"], item["fav_nums"]))
        self.conn.commit()
 
 
class MysqlTwistedPipline(object):
    def __init__(self, dbpool):
        self.dbpool = dbpool
 
    @classmethod
    def from_settings(cls, settings):
        dbparms = dict(
            host = settings["MYSQL_HOST"],
            db = settings["MYSQL_DBNAME"],
            user = settings["MYSQL_USER"],
            passwd = settings["MYSQL_PASSWORD"],
            charset='utf8',
            cursorclass=MySQLdb.cursors.DictCursor,
            use_unicode=True,
        )
        dbpool = adbapi.ConnectionPool("MySQLdb", **dbparms)
 
        return cls(dbpool)
 
    def process_item(self, item, spider):
        #使用twisted将mysql插入变成异步执行
        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):
        #执行具体的插入
        #根据不同的item 构建不同的sql语句并插入到mysql中
        insert_sql, params = item.get_insert_sql()
        print (insert_sql, params)
        cursor.execute(insert_sql, params)







      本文转自ning1022 51CTO博客,原文链接:http://blog.51cto.com/ning1022/1925593,如需转载请自行联系原作者



相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
25天前
|
调度 Python
16 Tornado - 认识异步
16 Tornado - 认识异步
12 1
|
1月前
|
数据采集 存储 数据库
异步爬虫实战:实际应用asyncio和aiohttp库构建异步爬虫
异步爬虫实战:实际应用asyncio和aiohttp库构建异步爬虫
|
4月前
|
JavaScript 前端开发 UED
同步和异步区别
同步和异步区别
42 0
|
4月前
|
前端开发 JavaScript UED
|
4月前
同步和异步的区别
同步和异步的区别
23 0
|
8月前
|
数据采集
使用aiohttp库实现异步爬虫进行优化
这篇文章我们详细介绍aiohttp库的用法和爬取实战
|
9月前
|
网络协议 测试技术 API
Python异步: 什么是异步? (2)
广义上,asyncio 是指使用协程在 Python 中实现异步编程的能力。
71 0
|
9月前
|
SQL 网络协议 安全
Python异步: 什么时候使用异步?(3)
从广义上讲,Asyncio 是新的、流行的、讨论广泛的和令人兴奋的。然而,对于何时应该在项目中采用它存在很多困惑。
75 0
|
9月前
|
消息中间件 前端开发 数据库
同步与异步详细区别
还在等什么,快来一起讨论关注吧,公众号【八点半技术站】,欢迎加入社群
|
11月前
|
前端开发
异步转同步的几种方法
在循环等待中,我们可以使用一个变量来指示异步操作是否已完成。然后,我们可以在循环中检查该变量,如果它指示异步操作已完成,则退出循环。
366 0
推荐文章
更多