scrapy 管道里面使用mysql插入数据库
2024-08-30 09:36:55
# -*- coding: utf-8 -*- # Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql class QuotesPipeline(object): def __init__(self):
self.conn = pymysql.connect(host='xxxxxxx', user='root', passwd='xxxxxxx', db='spider', charset='utf8')
self.cur = self.conn.cursor() def open_spider(self,spider):
print('spider start') def process_item(self, item, spider): try:
import time
# 格式化成2016-03-20 11:45:39形式
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
sql = "insert ignore into test(author,content,created_at) values('{}','{}','{}')".format(item['author'],item['content'],now)
reCount = self.cur.execute(sql)
self.conn.commit()
except Exception as e:
print(str(e)) return item
pip install pymsql
最新文章
- 使用nodejs爬取拉勾苏州和上海的.NET职位信息
- Python之路 day2 文件基础操作
- 在VS中添加lib的简单方法
- KeepAlived主备模型高可用LVS
- 动态加载HTML后使用query修改标签样式
- kafka 生产者java编码
- iOS开发中常用第三方库的使用和配置-GDataXML
- :Hibernate逍遥游记-第16管理session和实现对话
- 【Cocos2d-X开发学习笔记】第09期:渲染框架之菜单类(CCMenu)的使用
- HTTP协议学习-02
- 常用颜色RGB、灰度值
- 一键安装Cloud Torrent
- Python2 编码问题分析
- latex 一些使用
- Apache Hadoop学习笔记一
- SQL 经典应用
- google的python语言规范
- JavaWeb学习 (十八)————JSP标签
- January 20th, 2018 Week 3rd Saturday
- Python--Redis实战:第四章:数据安全与性能保障:第7节:非事务型流水线
热门文章
- CentOS7下NFS服务安装及配置固定端口
- BATJ都爱问的多线程面试题
- Hive学习笔记——metadata
- 工控随笔_24_关于西门子Step7的Simatic manager打开报3280:503错误。
- Mac Pro 2015休眠掉电解决办法
- FLINK-11738
- PHP防止刷微信红包方法
- 《ucore lab1 exercise4》实验报告
- Fineui 实现点击左边树状主菜单链接 打开新窗口或打开多个同一个tab
- 'cross-env' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 npm ERR! code ELIFECYCLE npm ERR! errno 1