zoukankan      html  css  js  c++  java
  • python scrapy 插入数据库的操作

    需要安装这个
    pymysql
    写法还是很简单的

    #
    -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import pymysql import douban.settings as settings class DoubanPipeline(object): def __init__(self): # 连接数据库 self.connect = pymysql.connect( host=settings.MYSQL_HOST, db=settings.MYSQL_DBNAME, user=settings.MYSQL_USER, passwd=settings.MYSQL_PASSWD, charset='utf8', use_unicode=True) # 通过cursor执行增删查改 self.cursor = self.connect.cursor(); def process_item(self, item, spider): #数据库的操作 try: # 插入数据 self.cursor.execute( """insert into course(name,teacher, content, comment ) value (%s, %s, %s, %s)""", ( item['bookName'], item['author'], item['score'], item['comment'])) # 提交sql语句 self.connect.commit() except Exception as e: print("错误在这里>>>>>>>>>>>>>",e,"<<<<<<<<<<<<<错误在这里") #写入文件txt with open('xuetang-0823.txt','a',encoding='utf-8') as fl: fl.write(str(item['xuhao'])+','+item['bookName']+',' +item['author']+',' +item['score'].replace(" ","").replace(" ","").replace(",","").replace(" ","")+','+item['comment'].replace(" ","").replace(" ","").replace(" ","")+' ')
  • 相关阅读:
    第二阶段冲刺(一)
    第一冲刺阶段博客检查汇总
    整改方案
    意见
    站立会议(九)
    站立会议八
    我爱淘二次冲刺阶段1
    我爱淘冲刺阶段7
    我爱淘冲刺阶段6
    我爱淘冲刺阶段5
  • 原文地址:https://www.cnblogs.com/dzhou/p/9523984.html
Copyright © 2011-2022 走看看