• 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(" ","")+' ')
  • 相关阅读:
    求解一元二次方程
    常用电脑软件
    c语言的布尔量
    unsigned int数据类型最大数
    int数据类型的最大数
    习题6-8 统计一行文本的单词个数
    习题6-6 使用函数输出一个整数的逆序数
    习题6-5 使用函数验证哥德巴赫猜想
    习题6-4 使用函数输出指定范围内的Fibonacci数
    C#委托、泛型委托
  • 原文地址:https://www.cnblogs.com/dzhou/p/9523984.html
Copyright © 2020-2023  润新知