爬虫数据插入数据库

爬虫数据插入数据库

def save_mysql(self,title,year1,km,money,label,pic):
    mydb=pymysql.connect(host='localhost',user='root',passwd='3863599',database='pachong')
    cursor=pymysql.cursors.DictCursor
    print(mydb)

    mycon=mydb.cursor()

    mycon.execute('create table IF NOT EXISTS guazi(id INT AUTO_INCREMENT PRIMARY KEY ,title VARCHAR(255) ,year1 VARCHAR(255),km VARCHAR(255),money VARCHAR(255),label VARCHAR(255),pic VARCHAR(255))')
    # mycon.execute('show tables')
    mycon.execute('select * from guazi')
    for i in mycon:
        print(i)
    print((title,year1,km,money,label,pic))
    sql='insert into guazi (title,year1,km,money,label,pic)VALUES (%s,%s,%s,%s,%s,%s)'
    val=[(title,year1,km,money,label,pic)]
    mycon.executemany(sql,val)
    print(mycon.rowcount,'记录插入成功')
    # for i in mycon:
    #     print(i)
    mydb.commit()

猜你喜欢

转载自blog.csdn.net/weixin_44200535/article/details/88785688