python-数据库

时间:2019-11-17
本文章向大家介绍python-数据库,主要包括python-数据库使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
# # 用python操作数据库
# import pymysql #先安装mysql模块,pip install pymysql
# DATSBASE={
# 'host':'12.0.0.1',
# 'database':'',
# 'user':'root',
# 'password':'wangwei',
# 'charset':'utf-8mb4'
# }
# db=pymysql.connect( 'host', 'database','user', 'password')
# #等价于
# db=pymysql.connect(**DATSBASE)
#
# # 游标:游标(cursor):系统为用户开通的一个数据缓冲区,用于存放SQL语句执行结果。用户使用的sql会逐一的在游标中获取记录,并赋值给主变量,
# # 交由Python进一步处理,一组主变量只能存放一条记录。
# #查询
# curson =pymysql.cursors()
# sql="select * from student"
# curson.execute(sql)
# results=curson.fetchall()#接收游标缓冲区返回的数据
# for row in results:
# print(row)
#
# #插入
# sql="insert into 'class'('name') values ('高一五班')";
# cursor =db.cursor()
# cursor.execute(sql)
# db.commit() #数据操纵语句,插入,删除,修改数据都需要进行commit

#捕捉异常
# try:
# a=10
# b=a+'hello'
# except TypeError as e:
# print("字符串类型与整型不可以相加")

# 编写python爬虫并保存到数据库
import requests #用来获取页面内容的模块
from bs4 import BeautifulSoup #pip install bs4
url="https://testerhome.com/"
response=requests.get(url)
soup=BeautifulSoup(response.text,'html.parser')#使返回的html代码变得整齐
# print(response.text)
print(soup)

原文地址:https://www.cnblogs.com/testerling/p/11876626.html