栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

scrapy Pipline保存到数据库

scrapy Pipline保存到数据库

import json
import pymssql
class TqybPipeline:
    # #保存到当前路径
    # def open_spider(self,spider):
    #      self.fp=open("天气.txt",'w',encoding='utf-8')
    # def process_item(self, item, spider):
    #     self.fp.write(json.dumps(dict(item),ensure_ascii=False)+"n")
    #     return item
    # def close_spider(self,spider):
    #      self.fp.close()
    def __init__(self):
        self.conn = pymssql.connect(server='192.168.1.104',port=1433,user='sa',password='******',database='stuDB',charset='UTF-8')
        self.cursor = self.conn.cursor()
    def process_item(self,item,spider):
        sql = 'insert into tianqi(rq,tq,tem,dw,city) values(%s,%s,%s,%s,%s)'
        self.cursor.execute(sql,(item['day'],item['tq'],item['gw'],item['dw'],item['city']))
        # sql ='select * from tianqi'
        # self.cursor.execute(sql)
        self.conn.commit()
        return item
    def spider_close(self,spider):
        self.cursor.close()
        self.conn.close()
在setting中添加
ITEM_PIPELINES = {
    'tqyb.pipelines.TqybPipeline': 300,

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/334902.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号