前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >scrapy数据入库PGsql

scrapy数据入库PGsql

原创
作者头像
待你如初见
修改2019-04-08 11:06:51
2.2K0
修改2019-04-08 11:06:51
举报
文章被收录于专栏:待你如初见待你如初见

pipelines.py

  • 在pipelines中有一个类如下图
image.png
image.png
  • 在类中创建方法open_spider
代码语言:txt
复制
 def open_spider(self, spider):
    hostname = '192.168.12.130'
    username = 'postgres'
    password = 'postgres'
    database = 'weibo'
    # 创建连接
    self.connection = psycopg2.connect(host=hostname, user=username, password=password, dbname=database)
    self.cur = self.connection.cursor()
  • 在process_item中填入代码
代码语言:txt
复制
def process_item(self, item, spider):
    try:
        self.cur.execute("INSERT INTO weibospider(mid,numberpage,isspider,creattime) VALUES (%s,%s,%s,now());",
                         (item['mid'], item['number_page'], item['is_spider']))
    except Exception as e:
        log.msg(e, level=log.ERROR)
    return item
  • 在类中创建方法close_spider
代码语言:txt
复制
def close_spider(self, spider):
    self.cur.close()
    self.connection.close()
  • setting.py中的配置指定
代码语言:txt
复制
pipelinesITEM_PIPELINES = {

   'WeiBoSpider.pipelines.WeibospiderPipeline': 300,

}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • pipelines.py
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档