scrapy 管道里面使用mysql插入数据库 python操作mysql
2024-10-09 19:19:18
# -*- 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
最新文章
- 如何在Linux服务器中隐藏PHP版本
- CSS3之3D变换实例详解
- android开源项目和框架
- [LeetCode]题解(python):091 Decode Ways
- hdu 2426 Interesting Housing Problem 最大权匹配KM算法
- 【UML】——为什么要使用UML
- 关于oracle的函数,存储过程,触发器,序列,视图,左右连接一些的应用 带案例
- PID204 / 特种部队
- CentOS 6使用VNC配置远程桌面
- C#单元测试工具包:MvcContrib
- SQL注入(三)
- 在用python操作mysql时报错:ModuleNotFoundError: No module named 'MySQLdb'
- ArcGIS 网络分析[2.1] 最短路径
- java垃圾回收GC
- Django--缓存设置
- pascal与其它语言代码书写的不同和pascal的快捷键
- 20.pipe
- C#计算机性能参数
- CVE-2010-2553 Microsoft Windows Cinepak 编码解码器解压缩漏洞 分析
- Oracle查询一个表的数据插入到另一个表