【Python网络爬虫】150讲轻松搞定Python网络爬虫付费课程笔记 篇十六——数据存储:MySQL

网络爬虫课程已经进入到 MySQL 存储部分啦,继续加油!

这篇博客会介绍爬虫过程中对MySQL的使用,这里面就不对MySQL的安装做过多介绍,主要是python对MySQL的操作。

MySQL基本操作:https://blog.csdn.net/weixin_44566432/article/details/106025116

1. MySQL的驱动程序

python 操作MySQL需要依赖一个中间件,即驱动程序,它可以是

  • mysqldb, python2中,目前已经停止维护
  • Mysqlclient
  • Pymysql,这里选择pymysql

2. MySQL 连接

3. MySQL插入

4. MySQL查找

import pymysql

# 1. 使用pymysql.connet方法连接数据库
db = pymysql.connect(host='localhost', user='root', password='123456', database='csdn_crawler')

# 中文,charset 指定 utf8, 不是utf-8

# 2. 使用cursor操作db
cursor = db.cursor()
#
cursor.execute("select * from article")
result = cursor.fetchone()
# print(result)
result2 = cursor.fetchall()
# print(result2)
result3 = cursor.fetchmany(4)
print(result3)

# 3. 插入数据
# sql = "insert into article(id, title, content) values (4 , 'hi', 'hello')"
#
# # sql = "insert into article(id, title, content) values (null, %s, %s)"
#
# cursor.execute(sql)
# db.commit()
# db.close()

猜你喜欢

转载自blog.csdn.net/weixin_44566432/article/details/108748762