爬虫常用mysql

1.导出数据

mysqldump -u root -p123456 tiantian > C:\Users\ASUS\Desktop\shangduogou.sql    

:然后输入密码

mysqldump -u dbuser -p dbname > dbname.sql

2.操作数据库

      import pymysql
        title = item[‘title‘]
        course = item[‘course‘]
        knobble = item[‘knobble‘]
        study_count = item[‘study_count‘]
        href = item[‘href‘]
        db = pymysql.connect(
            host="127.0.0.1",
            port=3306,
            user="root",
            password="123456",
            database="wyzx",
            charset="utf8",
            cursorclass=pymysql.cursors.DictCursor
        )
        # 拿到游标
        cursor = db.cursor()
        res1 = cursor.execute(f‘select id from app_title where title="{title}";‘)
        title_id = (cursor.fetchone()).get("id")
        # res2 = cursor.execute(f‘select id from detail where image_url="{image_url}";‘)
        # detail_id = (cursor.fetchone()).get("id")
        res3 = cursor.execute(
            f‘insert into app_coursedetail(course,knobble,study_count,href,title_id) values("{course}","{knobble}","{study_count}","{href}","{title_id}");‘)
        db.commit()
        cursor.close()
        db.close()
        print(title_id)

相关推荐