scrapy 管道里面使用mysql插入数据库
# -*- 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
scrapy 管道里面使用mysql插入数据库的更多相关文章
- scrapy 管道里面使用mysql插入数据库 python操作mysql
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to t ...
- Mybatis+Mysql插入数据库返回自增主键id值的三种方法
一.场景: 插入数据库的值需要立即得到返回的主键id进行下一步程序操作 二.解决方法: 第一种:使用通用mapper的插入方法 Mapper.insertSelective(record): 此方法: ...
- MySql 插入数据库报错 Incorrect string value: '\xF0\xA0\x86\xA2'
今天从nginx日志分析搜索关键字,然后把关键字插入到Mysql数据库里,出现如下错误 SQL state [HY000]; error code [1366]; Incorrect string v ...
- scrapy数据存储在mysql数据库的两种方式
方法一:同步操作 1.pipelines.py文件(处理数据的python文件) import pymysql class LvyouPipeline(object): def __init__(se ...
- MYSQL批量插入数据库实现语句性能分析
假定我们的表结构如下 代码如下 CREATE TABLE example ( example_id INT NOT NULL, name VARCHAR( 50 ) NOT NULL, value ...
- PHP+Mysql————表单数据插入数据库及数据提取
站点在进行新用户注冊时,都会将用户的注冊信息存入数据库中,须要的时候再进行提取.今天写了一个简单的实例. 主要完毕下面几点功能: (1)用户进行注冊,实现password反复确认,验证码校对功能. ( ...
- mysql去除严格模式/插入数据库遇到重复保证唯一
1.找到mysql目录下的数据库的my.ini文件.找到sql-mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION ...
- MYSQL批量插入数据库实现语句性能分析【转】 批量插入!程序里面对于数据库插入的功能尽量用【异步处理+批量插入+(事务)】
假定我们的表结构如下 代码如下 CREATE TABLE example (example_id INT NOT NULL,name VARCHAR( 50 ) NOT NULL,value VA ...
- 往MySQL数据库datetime类型字段中插入数据库的当前时间
代码: StringBuilder sb = new StringBuilder(); sb.append(" insert into uosdetailfile ("); sb. ...
随机推荐
- JS的base64编码解码
Unicode问题解法 有个小坑是它只支持ASCII. 如果你调用btoa("中文")会报错: Uncaught DOMException: Failed to execute ' ...
- ubuntu连接多个realsense d435
ubuntu连接多个realsense d435 import pyrealsense2 as rs import numpy as np import cv2 import time import ...
- openvswitch2.11.0修改源码后重新编译(2)
一:前提 已经正常安装了SDN环境(mininet和openswitch2.11.0和Ryu) 使用前面教程安装环境SDN实验---使用git安装Mininet (一)测试ovs是否正常使用 1.ry ...
- Python中,我该如何切分字符串后保留分割符?
原文来源:https://stackoverflow.com/questions/2136556/in-python-how-do-i-split-a-string-and-keep-the-sepa ...
- git向远程git仓库提交代码步骤详解
一.从远程仓库clone工程到本地 git clone -b 分支名称 http://10.1.1.11/service/tmall-service.git localDestDirectory l ...
- 统一异常处理:HandlerExceptionResolver
SpringExceptionResolver.java package com.mmall.common; import com.mmall.exception.PermissionExceptio ...
- phpspreadsheet 中文文档 粗翻版
2019年10月11日09:32:33 官方使用文档 https://phpspreadsheet.readthedocs.io/en/stable/topics/accessing-cells/ ...
- mysql数据库每天备份
以备份mysql下的test数据库为例,备份到/home/data 使用mysqldunp命令 dump.sh mysqldump -h127. -uadmin -P3306 -ppassword t ...
- POJ-排序-归并排序与逆序对
排序:归并排序与逆序对 一.概念 归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序 ...
- jquery关于on click事件的理解
jquery关于on click事件的理解 <pre><a style="min-width:60px; margin-left:6px;" wenzhangid ...