zoukankan      html  css  js  c++  java
  • scrapy 管道里面使用mysql插入数据库 python操作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 

  • 相关阅读:
    DFS染色解决区域分块问题UVALive 6663
    栈之逆波兰
    线段树总结
    区间合并问题
    线段树的开闭区间问题
    离散化
    线段树的学习过程
    BFS的小结
    状态数组哪家强
    卡特兰数。
  • 原文地址:https://www.cnblogs.com/brady-wang/p/11792393.html
Copyright © 2011-2022 走看看