import pymysql
import redis, json, time
from scrapy.conf import settings
class DBPipeline:
def __init__(self):
# 连接数据库
self.rediscli = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
self.connect = pymysql.connect(
host='127.0.0.1',
database='douban',
port=3306,
user='root',
passwd='mysql',
charset='utf8')
# 通过cursor执行增删查改
self.cursor = self.connect.cursor()
self.rediscli.blpop('')
def process_item(self, item, spider):
try:
# 插入数据
self.cursor.execute(
"""insert into doubanmovie(title, other, author, score ,content)
value (%s, %s, %s, %s, %s)""",
(item['title'],
item['other'],
item['author'],
item['score'],
item['content']))
# 提交sql语句
self.connect.commit()
except Exception as error:
# 出现错误时打印错误日志
print(error)
return item
|