爬取的地址存入mysql记录

CREATE DATABASE HELLO;

CREATE TABLE IF NOT EXISTS `botoo`(
   `id` INT UNSIGNED AUTO_INCREMENT,
   `title` VARCHAR(100),
   `url` VARCHAR(100) ,
   `downloadUrl` VARCHAR(100) NOT NULL unique,
   `releaseTime` DATE,
   `state`  int(11) DEFAULT 1 , 
   `insertTime` DATE,
   PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;



insert into botoo (title,url,downloadUrl,releaseTime,insertTime)  values('测试','http://www.baidu.com','www.baidu.com','2019-11-22 10:00:00','2019-11-22 10:00:01');
import pymysql.cursors
import time 


def localTime():

    now = int(time.time()) 
    timeStruct = time.localtime(now) 
    strTime = time.strftime("%Y-%m-%d %H:%M:%S", timeStruct) 
    return strTime


def insertSqlLanguage(title,url,downloadUrl,releaseTime):
    insertDate = f"('{title}','{url}','{downloadUrl}','{releaseTime}','{localTime()}')"
    sqlLanguage = f"insert into sesese (title,url,downloadUrl,releaseTime,insertTime) values {insertDate}"
    return sqlLanguage


if __name__ == '__main__':

    connect= pymysql.Connect(    
            host='xxxxxxxx',
            user='xxxxx',
            password='xxxxxx',
            port=3306,
            db='xxxxxxxx',
            charset='utf8')
    cursor = connect.cursor()
    insertSql = insertSqlLanguage('测试','http://www.baidu.com','www.baidu.com','2019-11-22 10:00:00')

    cursor.execute(insertSql)
    connect.commit()

猜你喜欢

转载自www.cnblogs.com/botoo/p/11910459.html
今日推荐