[英]Python,Scrapy, Pipeline: function “process_item” not getting called
[英]Python Scrapy maintain SSH MySQL connection on every process_item
我還是Scrapy和Python的新手。
我能夠使用SSH憑據連接到遠程數據庫,但是...
希望防止此錯誤發生在每個被刮掉的物品上。
錯誤:2055:在'127.0.0.1:3306'上與MySQL服務器的連接丟失,系統錯誤:10053您主機中的軟件中止了已建立的連接
以下是我的MySQL Pipeline對象
import mysql.connector
import sshtunnel
class MySQLStorePipeline(object):
def __init__(self):
with sshtunnel.SSHTunnelForwarder(
('13.***.***.***', 22),
ssh_username='***',
ssh_password='***',
remote_bind_address=('db1.prod.***.***.net.***', 3306),
local_bind_address=('127.0.0.1', 3306)
) as tunnel:
self.dbcon = mysql.connector.connect(
host='127.0.0.1',
port=3306,
user='***',
database='***',
password='***',
charset='utf8'
)
self.cursor = self.dbcon.cursor()
def process_item(self, item, spider):
try:
tables = self.cursor.execute('SHOW TABLES')
print tables.fetchall()
self.dbcon.commit()
except mysql.connector.Error as err:
print('Error: {}'.format(err))
return item
我只是不知道如何在process_item函數中維護數據庫連接
您正在使用with ...
這就是為什么您會收到此行為的原因來自Python的SSH隧道自動關閉
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.