import pymysql
from scrapy.exceptions import DropItem
import time class ErshouchePipeline(object):
def __init__(self):
self.conn = pymysql.connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
passwd = 'mlpythonlmoi',
db = 'ershouche',
charset = 'utf8'
)
self.cusor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
sql1 = "select 路由网址 from 二手车之家"
result = self.cusor.execute(sql1)#读取已经爬取的数据url
# print(result)
temp = self.cusor.fetchall()#返回查询到的所有记录
print('返回查询得到的记录:',temp)
self.url_list = []
for i in temp:
self.url_list.append(i['路由网址'])
print('存在的:',self.url_list) def process_item(self, item, spider):
if item['car_url'] not in self.url_list:
sql = "insert into 二手车之家 values(Null,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
lst = (item['city'],item['trademark'],item['model'],item['colour'],item['price'],item['purpose'],
item['vehicle_condition'],item['drive_mode'],item['Truck_kilometer'],item['car_license'],
item['Stop_displacemen'],item['year_jian_due'],item['insurance_policy_matures'],item['assurance_due'],
item['emission_standard'],item['guohu_number'],item['maintenance'],item['car_url'])
self.cusor.execute(sql,lst)
self.conn.commit()
else:
raise DropItem('该item数据库中已经存在!')
return item def close_spider(self, spider):
self.cusor.close()
self.conn.close()#关闭连接
print("操作结束!")
print('结束时间:' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))

scrapy管道MySQL简记的更多相关文章

  1. scrapy 管道里面使用mysql插入数据库

    # -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to t ...

  2. scrapy 管道里面使用mysql插入数据库 python操作mysql

    # -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to t ...

  3. scrapy连接MySQL

    Scrapy中连接MySQL所需要做的工作如下: 1.settings中需要设置的部分 # 启动管道组件 ITEM_PIPELINES = { 'QianChengWuYu.mongoDBPiplel ...

  4. scrapy存储mysql

    scrapy 数据存储mysql   #spider.pyfrom scrapy.linkextractors import LinkExtractor from scrapy.spiders imp ...

  5. scrapy 实现mysql 数据保存

    开始用scrapy 爬取数据的时候  开始用同步操作始终会报1064  的错误  因为 mysql 语法和导入的字段不兼容 尝试了  n  次之后  开始用  异步爬取  虽然一路报错 但是还是能把数 ...

  6. scrapy操作mysql/批量下载图片

    1.操作mysql items.py meiju.py 3.piplines.py 4.settings.py -------------------------------------------- ...

  7. scrapy框架初识

    一.scrapy简介 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等)的具 ...

  8. scrapy爬取猫眼电影排行榜

    做爬虫的人,一定离不开的一个框架就是scrapy框架,写小项目的时候可以用requests模块就能得到结果,但是当爬取的数据量大的时候,就一定要用到框架. 下面先练练手,用scrapy写一个爬取猫眼电 ...

  9. scrapy wiki资料汇总

    See also: Scrapy homepage, Official documentation, Scrapy snippets on Snipplr Getting started If you ...

随机推荐

  1. PGIS大数据量点位显示方案

    PGIS大数据量点位显示方案 问题描述 PGIS在地图上显示点位信息时,随点位数量的增加浏览器响应速度会逐渐变慢,当同时显示上千个点时浏览器会变得非常缓慢,以下是进行的测试: 测试环境: 服务器: C ...

  2. 线性表的Java实现--链式存储(双向链表)

    有了单向链表的基础,双向链表的实现就容易多了. 双向链表的一般情况: 增加节点: 删除节点: 双向链表的Java实现: package com.liuhao.algorithm;      publi ...

  3. android红米等关于读取本地文件夹图片获取路径的问题的解决

    在Android开发中,有从本地文件夹中读取图片的功能,使用一下代码打开图片选择列表: Intent intent = new Intent();   intent.setAction(Intent. ...

  4. javascript 字面量

    https://www.cnblogs.com/radius/p/6874165.html

  5. 《深入理解mybatis原理》 Mybatis数据源与连接池

    对于ORM框架而言,数据源的组织是一个非常重要的一部分,这直接影响到框架的性能问题.本文将通过对MyBatis框架的数据源结构进行详尽的分析,并且深入解析MyBatis的连接池. 本文首先会讲述MyB ...

  6. linux下安装apache与php

    http://www.92csz.com/study/linux/16.htm 1.apache 在如下页面下载apache的for Linux 的源码包 http://www.apache.org/ ...

  7. December 07th 2016 Week 50th Wednesday

    Life is a flower, and love is the honey of the flower. 人生是花儿,而爱情就是花的蜜. My life is not as beautiful a ...

  8. python使用SQLAlchemy对mysql操作

    安装SQLAlchemy pip install sqlalchemy 在MySQL的test数据库中创建的user表,用SQLAlchemy来试试 数据库连接 第一步,导入SQLAlchemy,并初 ...

  9. DTCoreText 、WKWebView 、UIWebView的比较

    DTCoreText .WKWebView .UIWebView的比较 HTML->View 数据解析: WebCore:排版引擎核心,WebCore包含主要以下模块:Loader, Parse ...

  10. UVa 12661 - Funny Car Racing(Dijkstra)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...