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(" ","")+' ')
  • 相关阅读:
    abc
    与7无关的数
    字符串排序
    质因数的个数
    符号运算
    底层代码创建GUI
    图像处理基础---RGB图 灰度图 索引图 调色板
    82.游戏项目-椭圆轨迹的实现
    81.游戏项目-物体任意角度飞行和停止
    80.游戏项目-物体的移动
  • 原文地址:https://www.cnblogs.com/dzhou/p/9523984.html
Copyright © 2011-2022 走看看