【发布时间】:2020-01-09 15:53:45
【问题描述】:
我是 Python 新手,我编写的代码没有按预期工作。我尝试通过 Scrapy Spider 将一个项目插入 MySQL 数据库,但是当我检查数据库时没有那个项目。
我的管道代码如下。
from __future__ import print_function
import mysql.connector
from mysql.connector import errorcode
class LyricsPipeline(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='lyrical'
)
self.curr = self.conn.cursor()
# def create_table(self):
# self.curr.execute("""DROP TABLE IF EXISTS llyrics""")
# self.curr.execute("""create table llyrics (
# title text,
# movie text,
# lyrics text
# )""")
def process_item(self, item, spider):
self.store_db(item)
return item
def store_db(self, item):
self: curr.execute("""insert into mylyrics values ('title','movie','lyrics')""", (
item['title'][0],
item['movie'][0],
item['lyrics'][0]
))
self.conn.commit()
我错过了什么,你能告诉我吗?
【问题讨论】:
-
变量
item是什么你这里没有定义。此外,它可能不会产生错误,因为您没有调用函数process_item...因此没有调用store_db()函数。 -
我建议修改您的代码以运行
store_db()函数并定义item,然后在必要时返回问题和错误 -
日志中是否有任何错误?