Python中Scrapy+adbapi提高數(shù)據(jù)庫(kù)寫入效率實(shí)現(xiàn)
一:twisted中的adbapi
數(shù)據(jù)庫(kù)pymysql的commit()和execute()在提交數(shù)據(jù)時(shí),都是同步提交至數(shù)據(jù)庫(kù),由于scrapy框架數(shù)據(jù)的解析和異步多線程的,所以scrapy的數(shù)據(jù)解析速度,要遠(yuǎn)高于數(shù)據(jù)的寫入數(shù)據(jù)庫(kù)的速度。如果數(shù)據(jù)寫入過(guò)慢,會(huì)造成數(shù)據(jù)庫(kù)寫入的阻塞,影響數(shù)據(jù)庫(kù)寫入的效率。
使用twisted異步IO框架,實(shí)現(xiàn)數(shù)據(jù)的異步寫入,通過(guò)多線程異步的形式對(duì)數(shù)據(jù)進(jìn)行寫入,可以提高數(shù)據(jù)的寫入速度。
1.1 兩個(gè)主要方法
adbapi.ConnectionPool:
創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)連接池對(duì)象,其中包括多個(gè)連接對(duì)象,每個(gè)連接對(duì)象在獨(dú)立的線程中工作。adbapi只是提供了異步訪問(wèn)數(shù)據(jù)庫(kù)的編程框架,再其內(nèi)部依然使MySQLdb這樣的庫(kù)訪問(wèn)數(shù)據(jù)庫(kù)。
dbpool.runInteraction(do_insert,item):
異步調(diào)用do_insert函數(shù),dbpool會(huì)選擇連接池中的一個(gè)連接對(duì)象在獨(dú)立線程中調(diào)用insert_db,其中參數(shù)item會(huì)被傳給do_insert的第二個(gè)參數(shù),傳給do_insert的第一個(gè)參數(shù)是一個(gè)Transaction對(duì)象,其接口與Cursor對(duì)象類似,可以調(diào)用execute方法執(zhí)行SQL語(yǔ)句,do_insert執(zhí)行后,連接對(duì)象會(huì)自動(dòng)調(diào)用commit方法
1.2 使用實(shí)例
from twisted.enterprise import adbapi
# 初始化數(shù)據(jù)庫(kù)連接池(線程池)
# 參數(shù)一:mysql的驅(qū)動(dòng)
# 參數(shù)二:連接mysql的配置信息
dbpool = adbapi.ConnectionPool('pymysql', **params)
# 參數(shù)1:在異步任務(wù)中要執(zhí)行的函數(shù)insert_db; # 參數(shù)2:給該函數(shù)insert_db傳遞的參數(shù) query = self.dbpool.runInteraction(self.do_insert, item)
# 在execute()之后,不需要再進(jìn)行commit(),連接池內(nèi)部會(huì)進(jìn)行提交的操作。
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)
二:結(jié)合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)
到此這篇關(guān)于Python中Scrapy+adbapi提高數(shù)據(jù)庫(kù)寫入效率實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Scrapy+adbapi數(shù)據(jù)庫(kù)寫入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))
- scrapy與selenium結(jié)合爬取數(shù)據(jù)(爬取動(dòng)態(tài)網(wǎng)站)的示例代碼
- scrapy自定義pipeline類實(shí)現(xiàn)將采集數(shù)據(jù)保存到mongodb的方法
- Python基于scrapy采集數(shù)據(jù)時(shí)使用代理服務(wù)器的方法
- scrapy數(shù)據(jù)存儲(chǔ)在mysql數(shù)據(jù)庫(kù)的兩種方式(同步和異步)
- Python使用scrapy采集數(shù)據(jù)過(guò)程中放回下載過(guò)大頁(yè)面的方法
- 淺談Scrapy網(wǎng)絡(luò)爬蟲框架的工作原理和數(shù)據(jù)采集
- python爬蟲scrapy基于CrawlSpider類的全站數(shù)據(jù)爬取示例解析
- 詳解Python之Scrapy爬蟲教程N(yùn)BA球員數(shù)據(jù)存放到Mysql數(shù)據(jù)庫(kù)
相關(guān)文章
使用python flask框架開發(fā)圖片上傳接口的案例詳解
剛領(lǐng)導(dǎo)安排任務(wù),需求是這樣的開發(fā)一個(gè)支持多格式圖片上傳的接口,并且將圖片壓縮,支持在線預(yù)覽圖片,下面小編分享下使用python flask框架開發(fā)圖片上傳接口的案例詳解,感興趣的朋友一起看看吧2022-04-04
使用 Python 實(shí)現(xiàn)微信消息的一鍵已讀的思路代碼
利用python可以實(shí)現(xiàn)微信消息的一鍵已讀功能,怎么實(shí)現(xiàn)呢?你肯定會(huì)想著很復(fù)雜,但是python的好處就是很多人已經(jīng)把接口打包做好了,只需要調(diào)用即可,今天通過(guò)本文給大家分享使用 Python 實(shí)現(xiàn)微信消息的一鍵已讀的思路代碼,一起看看吧2021-06-06
詳解Python連接MySQL數(shù)據(jù)庫(kù)的多種方式
這篇文章主要介紹了Python連接MySQL數(shù)據(jù)庫(kù)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Python3二分查找?guī)旌瘮?shù)bisect(),bisect_left()和bisect_right()的區(qū)別
這篇文章主要介紹了Python3二分查找?guī)旌瘮?shù)bisect(),bisect_left()和bisect_right()的區(qū)別,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
python神經(jīng)網(wǎng)絡(luò)Batch?Normalization底層原理詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)Batch?Normalization底層原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

