Python中Scrapy+adbapi提高數(shù)據(jù)庫寫入效率實現(xiàn)
一:twisted中的adbapi
數(shù)據(jù)庫pymysql的commit()和execute()在提交數(shù)據(jù)時,都是同步提交至數(shù)據(jù)庫,由于scrapy框架數(shù)據(jù)的解析和異步多線程的,所以scrapy的數(shù)據(jù)解析速度,要遠高于數(shù)據(jù)的寫入數(shù)據(jù)庫的速度。如果數(shù)據(jù)寫入過慢,會造成數(shù)據(jù)庫寫入的阻塞,影響數(shù)據(jù)庫寫入的效率。
使用twisted異步IO框架,實現(xiàn)數(shù)據(jù)的異步寫入,通過多線程異步的形式對數(shù)據(jù)進行寫入,可以提高數(shù)據(jù)的寫入速度。
1.1 兩個主要方法
adbapi.ConnectionPool:
創(chuàng)建一個數(shù)據(jù)庫連接池對象,其中包括多個連接對象,每個連接對象在獨立的線程中工作。adbapi只是提供了異步訪問數(shù)據(jù)庫的編程框架,再其內部依然使MySQLdb這樣的庫訪問數(shù)據(jù)庫。
dbpool.runInteraction(do_insert,item):
異步調用do_insert函數(shù),dbpool會選擇連接池中的一個連接對象在獨立線程中調用insert_db,其中參數(shù)item會被傳給do_insert的第二個參數(shù),傳給do_insert的第一個參數(shù)是一個Transaction對象,其接口與Cursor對象類似,可以調用execute方法執(zhí)行SQL語句,do_insert執(zhí)行后,連接對象會自動調用commit方法
1.2 使用實例
from twisted.enterprise import adbapi
# 初始化數(shù)據(jù)庫連接池(線程池) # 參數(shù)一:mysql的驅動 # 參數(shù)二:連接mysql的配置信息 dbpool = adbapi.ConnectionPool('pymysql', **params)
# 參數(shù)1:在異步任務中要執(zhí)行的函數(shù)insert_db; # 參數(shù)2:給該函數(shù)insert_db傳遞的參數(shù) query = self.dbpool.runInteraction(self.do_insert, item)
# 在execute()之后,不需要再進行commit(),連接池內部會進行提交的操作。 def do_insert(self, cursor, item): insert_sql = """ insert into qa_sample( need_id, need_question_uptime, need_title, need_title_describe, need_answer_uptime, need_answer) values (%s, %s, %s, %s, %s, %s) """ params = (item['need_id'], item['need_question_uptime'], item['need_title'], item['need_title_describe'], item['need_answer_uptime'], item['need_answer']) cursor.execute(insert_sql, params)
二:結合scrapy中的pipelines
# -*- coding: utf-8 -*- from twisted.enterprise import adbapi import pymysql # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html class QaSpiderPipeline(object): def process_item(self, item, spider): return item class MysqlTwistedPipeline(object): def __init__(self, dbpool): self.dbpool = dbpool @classmethod def from_settings(cls, settings): dbparams = dict( host=settings['MYSQL_HOST'], db=settings['MYSQL_DBNAME'], user=settings['MYSQL_USER'], passwd=settings['MYSQL_PASSWORD'], charset='utf8', cursorclass=pymysql.cursors.DictCursor, use_unicode=True ) dbpool = adbapi.ConnectionPool('pymysql', **dbparams) return cls(dbpool) def process_item(self, item, spider): query = self.dbpool.runInteraction(self.do_insert, item) def do_insert(self, cursor, item): insert_sql = """ insert into qa_sample( need_id, need_question_uptime, need_title, need_title_describe, need_answer_uptime, need_answer) values (%s, %s, %s, %s, %s, %s) """ params = (item['need_id'], item['need_question_uptime'], item['need_title'], item['need_title_describe'], item['need_answer_uptime'], item['need_answer']) cursor.execute(insert_sql, params)
到此這篇關于Python中Scrapy+adbapi提高數(shù)據(jù)庫寫入效率實現(xiàn)的文章就介紹到這了,更多相關Scrapy+adbapi數(shù)據(jù)庫寫入內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- 利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復)
- scrapy與selenium結合爬取數(shù)據(jù)(爬取動態(tài)網(wǎng)站)的示例代碼
- scrapy自定義pipeline類實現(xiàn)將采集數(shù)據(jù)保存到mongodb的方法
- Python基于scrapy采集數(shù)據(jù)時使用代理服務器的方法
- scrapy數(shù)據(jù)存儲在mysql數(shù)據(jù)庫的兩種方式(同步和異步)
- Python使用scrapy采集數(shù)據(jù)過程中放回下載過大頁面的方法
- 淺談Scrapy網(wǎng)絡爬蟲框架的工作原理和數(shù)據(jù)采集
- python爬蟲scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取示例解析
- 詳解Python之Scrapy爬蟲教程NBA球員數(shù)據(jù)存放到Mysql數(shù)據(jù)庫
相關文章
詳解如何從TensorFlow的mnist數(shù)據(jù)集導出手寫體數(shù)字圖片
這篇文章主要介紹了詳解如何從TensorFlow的mnist數(shù)據(jù)集導出手寫體數(shù)字圖片,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08python3使用urllib示例取googletranslate(谷歌翻譯)
這篇文章主要介紹了使用urllib取googletranslate(谷歌翻譯)的示例,通過這個谷歌翻譯示例學習python3中urllib的使用方法,2014-01-01python 裝飾器功能以及函數(shù)參數(shù)使用介紹
之前學習編程語言大多也就是學的很淺很淺,基本上也是很少涉及到裝飾器這些的類似的內容??偸怯X得是一樣很神奇的東西,舍不得學(嘿嘿)。今天看了一下書籍。發(fā)現(xiàn)道理還是很簡單的2012-01-01