鱼C论坛

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

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

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

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

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

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

我用的mysql数据库
最佳答案
2018-11-20 12:00:53
  1. import pymysql
  2. import redis, json, time
  3. from scrapy.conf import settings

  4. class DBPipeline:
  5.     def __init__(self):
  6.         # 连接数据库
  7.         self.rediscli = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
  8.         self.connect = pymysql.connect(
  9.             host='127.0.0.1',
  10.             database='douban',
  11.             port=3306,
  12.             user='root',
  13.             passwd='mysql',
  14.             charset='utf8')

  15.         # 通过cursor执行增删查改
  16.         self.cursor = self.connect.cursor()
  17.         self.rediscli.blpop('')

  18.     def process_item(self, item, spider):
  19.         try:
  20.             # 插入数据
  21.             self.cursor.execute(
  22.                 """insert into doubanmovie(title, other, author, score ,content)
  23.                 value (%s, %s, %s, %s, %s)""",
  24.                 (item['title'],
  25.                  item['other'],
  26.                  item['author'],
  27.                  item['score'],
  28.                  item['content']))

  29.             # 提交sql语句
  30.             self.connect.commit()

  31.         except Exception as error:
  32.             # 出现错误时打印错误日志
  33.             print(error)
  34.         return item
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

使用道具 举报

发表于 2018-11-20 12:00:53 | 显示全部楼层    本楼为最佳答案   
  1. import pymysql
  2. import redis, json, time
  3. from scrapy.conf import settings

  4. class DBPipeline:
  5.     def __init__(self):
  6.         # 连接数据库
  7.         self.rediscli = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
  8.         self.connect = pymysql.connect(
  9.             host='127.0.0.1',
  10.             database='douban',
  11.             port=3306,
  12.             user='root',
  13.             passwd='mysql',
  14.             charset='utf8')

  15.         # 通过cursor执行增删查改
  16.         self.cursor = self.connect.cursor()
  17.         self.rediscli.blpop('')

  18.     def process_item(self, item, spider):
  19.         try:
  20.             # 插入数据
  21.             self.cursor.execute(
  22.                 """insert into doubanmovie(title, other, author, score ,content)
  23.                 value (%s, %s, %s, %s, %s)""",
  24.                 (item['title'],
  25.                  item['other'],
  26.                  item['author'],
  27.                  item['score'],
  28.                  item['content']))

  29.             # 提交sql语句
  30.             self.connect.commit()

  31.         except Exception as error:
  32.             # 出现错误时打印错误日志
  33.             print(error)
  34.         return item
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-18 06:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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