zoukankan      html  css  js  c++  java
  • python论文爬取(三)

    pycharm连接数据并对数据增删改查

    Mysql.py

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import traceback
    
    import pymysql
    
    import pymysql.cursors
    
    import time
    
    # 获得conn cursor
    
    
    def get_conn():
    
        # 打开数据库连接
        conn = pymysql.connect(host='localhost',
                               user='root',
                               passwd='',
                               db='lunwen',
                               port=3306,
                               charset='utf8')
    
        # 创建游标,默认是元组型
        cursor = conn.cursor()
        return conn, cursor
    
    
    # 关闭conn cursor
    def close_conn(conn, cursor):
        if cursor:
            cursor.close()
        if conn:
            conn.close()
    
    
    def query(sql, *args):
        conn, cursor = get_conn()
        cursor.execute(sql, args)
        res = cursor.fetchall()
        close_conn(conn, cursor)
        return res
    
    
    def insert_item(item):
    
        cursor = None
        conn = None
        try:
            print(f"{time.asctime()}开始插入历史数据")
            conn, cursor = get_conn()
            sql = "insert into homework values(%s,%s,%s,%s,%s,%s)"
            for v in item:
                cursor.execute(sql, [v[0], v[1], v[2], v[3], v[4], v[5]])  # %Y-%m-%d %H:%M:%S
    
            conn.commit()
            print(f"{time.asctime()}插入历史数据完毕")
        except:
            traceback.print_exc()
        finally:
            close_conn(conn, cursor)

    mysqlUtil.py

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import traceback
    
    import pymysql
    
    import pymysql.cursors
    
    import time
    import Keyword
    
    
    # 获得conn cursor
    def get_conn():
    
        # 打开数据库连接
        conn = pymysql.connect(host='localhost',
                               user='root',
                               passwd='',
                               db='lunwen',
                               port=3306,
                               charset='utf8')
    
        # 创建游标,默认是元组型
        cursor = conn.cursor()
        return conn, cursor
    
    
    # 关闭conn cursor
    def close_conn(conn, cursor):
        if cursor:
            cursor.close()
        if conn:
            conn.close()
    
    
    def query(sql, *args):
        conn, cursor = get_conn()
        cursor.execute(sql, args)
        res = cursor.fetchall()
        close_conn(conn, cursor)
        return res
    
    
    def select_lunwenj(str, value):
            args = value
            sql = "select * from xiangxi where "+str+" = "+args
            print(sql)
            res = query(sql)
            return res
    
    
    def select_lunwenm(str, value):
        args = '%%' + value + '%%'
        sql = "select * from xiangxi where " + str + " like '%s'" % args
        print(sql)
        res = query(sql)
        return res
    
    
    def select_ab():
        sql = "select abstract from xiangxi "
        res = query(sql)
        return res
    
    
    def insert_key(word, count):
        cursor = None
        conn = None
        try:
            print(f"{time.asctime()}开始插入历史数据")
            conn, cursor = get_conn()
            sql = "insert into keyword values(%s,%s)"
            cursor.execute(sql, [word, count])  # %Y-%m-%d %H:%M:%S
    
            conn.commit()
            print(f"{time.asctime()}插入历史数据完毕")
        except:
            traceback.print_exc()
        finally:
            close_conn(conn, cursor)
    
    def select_key():
        sql = "select * from keyword "
        res = query(sql)
        return res
  • 相关阅读:
    tortoisegit 还原到某个版本
    AndroidStudio2021/3版 gradle7.0环境 自定义输出apk路径
    docker镜像打包save,载入load,启动run
    Failed to execute goal com.spotify:docker-maven-plugin:0.4.13:build (default-cli)
    windows10下载网址
    win10开启CPU虚拟化导致蓝屏信息
    swagger-ui.html
    git初始化
    node-sass 把变量导出给js共享。
    openlayers moveend getEventCoordinate
  • 原文地址:https://www.cnblogs.com/wangdayang/p/14914384.html
Copyright © 2011-2022 走看看