我正在尝试将已抓取的数据抓取存储到SQL数据库,但是我的代码没有发送任何内容,而在运行时未提及任何错误。
我正在使用sql连接器,因为我无法安装MySQL-python。我的SQL数据库似乎运行良好,当我运行代码时,流量增加了KB / s。请在下面找到我的pipelines.py代码。
import mysql.connector
from mysql.connector import errorcode
class CleaningPipeline(object):
...
class DatabasePipeline(object):
def _init_(self):
self.create_connection()
self.create_table()
def create_connection(self):
self.conn = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '********',
database = 'lecturesinparis_db'
)
self.curr = self.conn.cursor()
def create_table(self):
self.curr.execute("""DROP TABLE IF EXISTS mdl""")
self.curr.execute("""create table mdl(
title text,
location text,
startdatetime text,
lenght text,
description text,
)""")
def process_item(self, item, spider):
self.store_db(item)
return item
def store_db(self, item):
self.curr.execute("""insert into mdl values (%s,%s,%s,%s,%s)""", (
item['title'][0],
item['location'][0],
item['startdatetime'][0],
item['lenght'][0],
item['description'][0],
))
self.conn.commit()
您需要首先添加该类,ITEM_PIPELINES
以便让沙哑的人知道我要使用此管道。
在您的settings.py
文件中,使用您的类名称更新以下行,如下所示。
# https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'projectname.pipelines.CleaningPipeline': 700,
'projectname.pipelines.DatabasePipeline': 800,
}
数字700和800表示管道将按什么顺序处理数据,它可以是1-1000之间的任何整数。管道将根据此数字按顺序处理项目,因此,具有700的管道将在具有800的管道之前处理数据。
注意:将项目名称替换为'projectname.pipelines.CleaningPipeline'
您的实际项目名称。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句