鱼C论坛

 找回密码
 立即注册
查看: 542|回复: 2

[已解决]scrapy框架爬数据怎么保存数据库

[复制链接]
发表于 2018-11-20 08:39:26 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
框架爬取的数据怎么保存到数据库啊,跪求大佬。可以的话麻烦备注写的详细些,代码直接发上来把,我自己调试调试琢磨琢磨

我用的mysql数据库
最佳答案
2018-11-20 12:00:53
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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-11-20 10:09:13 | 显示全部楼层
随便到B站看个scrapy视频都有讲解啦,
文档都有写的,看看就好。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-20 12:00:53 | 显示全部楼层    本楼为最佳答案   
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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-16 17:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表