第四篇: python操作数据库
数据入库+爬虫
import requests
from bs4 import BeautifulSoup
import pymysql
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='pachong',
charset='utf8',
autocommit=True
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
ret = requests.get('https://www.autohome.com.cn/news/2/#liststart')
print(ret.status_code)
soup = BeautifulSoup(ret.text, 'html.parser')
li_list = soup.find_all(name='li')
for li in li_list:
h3 = li.find(name='h3')
if not h3:
continue
title = h3.text
desc = str(li.find(name='p').text)
img = 'https:' + li.find(name='img')['src']
url = 'https:' + li.find(name='a')['href']
print(f'''
新闻标题:{title}
新闻摘要:{desc}
新闻链接:{url}
新闻图片:{img}
''')
sql = 'insert into cat_family(title,content,url,img) values(%s,%s,%s,%s)'
cursor.execute(sql, (title, desc, url, img))
cursor.close()
conn.close()
一、python操作数据库
import pymysql
conn = pymysql.connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
password = '123',
database = 'jeff',
charset = 'utf8',
autocommit = True # 自动提交
)
cursor = conn.cursor(pymysql.cursors.DictCursor) # 产生游标对象,将查询出来的结果制作成字典的形式返回
sql = 'select * from teacher'
cursor.execute(sql) # 执行传入的sql语句
# print(cursor.fetchone()) # 只获取一条数据
# print(cursor.fetchone()) # 只获取一条数据
# print(cursor.fetchone()) # 只获取一条数据
# print(cursor.fetchone()) # 只获取一条数据
# cursor.scroll(2,'absolute') # 绝对定位,控制光标移动 absolute相对于其实位置 往后移动几位
# cursor.scroll(1,'relative') # 相对定位,relative相对于当前位置 往后移动几位
print(cursor.fetchall()) # 获取所有的数据 返回的结果是一个列表
cursor.close()
conn.close()
二、数据注入问题
正常演示
import pymysql
conn = pymysql.connect(
user = 'root',
passwd = '123',
db = 'jeff',
host = '127.0.0.1',
port = 3306,
charset = 'utf8'
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 获取用户输入的用户名和密码 然后取数据库中校验
username = input('username>>>:').strip()
password = input('password>>>:').strip()
sql = "select * from userinfo where username='%s' and password= '%s'"%(username,password)
print(sql)
cursor.execute(sql)
res = cursor.fetchall()
if res:
print(res)
else:
print('用户名或密码错误!')
正常操作:
复原sql:正常
注入演示
利用mysql中的'--'注释符号,注入
不输入密码也能获取数据:
复原sql:密码验证同样被注释了
同样,不输入用户名和密码注入:
复原sql语句:后面的内容被注释了
三、解决方法
利用特殊符号和注释语法 巧妙的绕过真正的sql校验
关键性的数据 不要自己手动去拼接 而是交由execute帮你去做拼接
execute:可以自动过滤"--"类似这种的特殊符号,还能自动识别“%s”,帮你自动拼接
cursor.execute(sql, (username, password))
import pymysql
conn = pymysql.connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
password = '123',
database = 'jeff',
charset = 'utf8',
autocommit = True # 这个参数配置完成后 增删改操作都不需要在手动加conn.commit了
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
username = input('输入用户名>>>:')
password = input('输入密码>>>:')
sql = 'select * from user where name = %s and password = %s'
res = cursor.execute(sql, (username, password)) # 能够帮你自动过滤特殊符号 避免sql注入的问题
if res :
print(cursor.fetchall())
else:
print('用户名或密码错误')
过滤成功,sql注入失败:
四、python对数据库的增删改查
# 手动提交
conn.commit() # 确认当前操作 真正的同步到数据库
import pymysql
conn = pymysql.connect(
user = 'root',
passwd = '123',
db = 'jeff',
host = '127.0.0.1',
port = 3306,
charset = 'utf8',
autocommit = True # 自动提交确认
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 生成游标对象
增
# 增
sql = "insert into userinfo(username,password)values('tank',789)"
res = cursor.execute(sql) # 被影响的行数
print(res)
改
# 改
sql = "update userinfo set name='jeff_gyy' where id = 1"
res = cursor.execute(sql) # 被影响的行数
print(res)
删
# 删
sql = "delete from userinfo where id= 1"
res = cursor.execute(sql) # 被影响的行数
print(res)