- Mysql数据库的基本操作
- 用python操作数据库
- 编写python爬虫并保存到数据库
我们平时说到的数据库,指的是 数据库管理系统
MariaDB: MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品
另外一种类型的数据库是:非关系型数据库。 比较流行的是:Mongodb, redis
In [46]:
    
import json
data_1 = "{'a': 1, 'b': 2, 'c': 3}"
data_2 = '{"a": 1, "b": 2, "c": 3}'
j_data = json.loads(data_2)
type(j_data)
with open('/Users/wangyujie/Desktop/data.json', 'r') as f:
    j_data = json.load(f)
    print(j_data)
    
    
json格式
In [20]:
    
# 链接数据库? 
mysql -u root -p 
# u 是用户名 p: 需要用密码登录数据库
# 查看数据库
show databases;
# 选择数据库
use database_name;
# 查看数据库中的table表
show tables;
# 查看表格的结构
desc tables;
# 查看表中的数据
select * from table_name;
# 查看数据并限制数量
select * from table_name limit 10;
    
    
sequelpro 链接:http://www.sequelpro.com/
| 姓名 | 性别 | 年龄 | 班级 | 考试 | 语文 | 数学 | 英语 | 物理 | 化学 | 生物 | 
|---|---|---|---|---|---|---|---|---|---|---|
| 高海 | 男 | 18 | 高三一班 | 第一次模拟 | 90 | 126 | 119 | 75 | 59 | 89 | 
| 高海 | 男 | 18 | 高三一班 | 第二次模拟 | 80 | 120 | 123 | 85 | 78 | 87 | 
| 秦佳艺 | 女 | 18 | 高三二班 | 第一次模拟 | 78 | 118 | 140 | 89 | 80 | 78 | 
| 秦佳艺 | 女 | 18 | 高三二班 | 第二次模拟 | 79 | 120 | 140 | 83 | 78 | 82 | 
In [ ]:
    
3 转换为二进制 11 整数部分
0.4 转换为二进制 0.5*0 + 0.25*1 + 0.125*1 
                    0 1 1 1 1 0 1
    
In [ ]:
    
insert into `class`(`id`, `name`) 
values(1, '高一三班');
    
In [ ]:
    
update `class` set `name` = '高一五班'
where `name` = '高一三班';
    
In [ ]:
    
delete from `class`
where `id` = 6;
    
In [48]:
    
import MySQLdb
    
In [58]:
    
DATABASE = {
    'host': '127.0.0.1', # 如果是远程数据库,此处为远程服务器的ip地址
    'database': 'Examination',
    'user': 'root',
    'password': 'wangwei',
    'charset': 'utf8mb4'
}
db = MySQLdb.connect(host='localhost', user='root', password='wangwei', db='Examination')
# 等价于
db = MySQLdb.connect('localhost', 'root', 'wangwei', 'Examination')
# 等价于
db = MySQLdb.connect(**DATABASE)
# db就代表是我们的数据库
    
In [59]:
    
cursor = db.cursor()
    
In [65]:
    
sql = "select * from student where id <= 20 limit 4"
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
    print(row)
    
    
In [68]:
    
sql = "insert into `class`(`name`) values('高一五班');"
cursor = db.cursor()
cursor.execute(sql)
cursor.execute(sql)
db.commit()
    
In [69]:
    
sql = "delete from `class` where `name`='高一五班'"
cursor = db.cursor()
cursor.execute(sql)
db.commit()
    
In [70]:
    
sql = "update `class` set `name`='高一十四班' where `id`=4;"
cursor = db.cursor()
cursor.execute(sql)
db.commit()
    
In [73]:
    
a = 10
b = a + 'hello'
    
    
In [74]:
    
try:
    a = 10
    b = a + 'hello'
except TypeError as e:
    print(e)
    
    
In [80]:
    
try:
    sql = "insert into `class`(`name`) values('高一十六班')"
    cursor = db.cursor()
    cursor.execute(sql)
    error = 10 + 'sdfsdf'
    db.commit()
except Exception as e:
    print(e)
    db.rollback()
    
    
1、requests 用来获取页面内容
2、BeautifulSoup
In [160]:
    
import time
import MySQLdb
import requests
from bs4 import BeautifulSoup
    
In [168]:
    
# 此处为数据库配置文件,每个人的配置不同,因此需要同学们自己配置
DATABASE = {
    'host': '127.0.0.1', # 如果是远程数据库,此处为远程服务器的ip地址
    'database': '',
    'user': '',
    'password': '',
    'charset': 'utf8mb4'
}
    
In [164]:
    
# 获取url下的页面内容,返回soup对象
def get_page(url):
    responce = requests.get(url)
    soup = BeautifulSoup(responce.text, 'lxml')
    return soup
# 封装成函数,作用是获取列表页下面的所有租房页面的链接,返回一个链接列表
def get_links(link_url):
    soup = get_page(link_url)
    links_div = soup.find_all('div', class_="pic-panel")
    links = [div.a.get('href') for div in links_div]
    return links
def get_house_info(house_url):
    soup = get_page(house_url)
    price = soup.find('span', class_='total').text
    unit = soup.find('span', class_='unit').text.strip()
    house_info = soup.find_all('p')
    area = house_info[0].text[3:]
    layout = house_info[1].text[5:]
    floor = house_info[2].text[3:]
    direction = house_info[3].text[5:]
    subway = house_info[4].text[3:]
    community = house_info[5].text[3:]
    location = house_info[6].text[3:]
    create_time = house_info[7].text[3:]
    agent = soup.find('a', class_='name LOGCLICK')
    agent_name = agent.text
    agent_id = agent.get('data-el')
    evaluate = soup.find('div', class_='evaluate')
    score, number = evaluate.find('span', class_='rate').text.split('/')
    times = evaluate.find('span', class_='time').text[5:-1]
    info = {
            '价格': price,
            '单位': unit,
            '面积': area,
            '户型': layout,
            '楼层': floor,
            '朝向': direction,
            '发布时间': create_time,
            '地铁': subway,
            '小区': community,
            '位置': location,
            '经纪人名字': agent_name,
            '经纪人id': agent_id
    }
    return info
def get_db(setting):
    return MySQLdb.connect(**setting)
def insert(db, house):
    values = "'{}',"* 10 + "'{}'"
    sql_values = values.format(house['价格'],house['单位'],house['面积'],house['户型'],
                              house['楼层'],house['朝向'],house['地铁'],house['小区'],
                              house['位置'],house['经纪人名字'],house['经纪人id'])
    sql = """
        insert into `house`(`price`, `unit`, `area`, `layout`, `floor`, `direction`,
        `subway`, `community`, `location`, `agent_name`, `agent_id`)
        values({})
    """.format(sql_values)
    print(sql)
    cursor = db.cursor()
    cursor.execute(sql)
    db.commit()
    
In [167]:
    
db = get_db(DATABASE)
links = get_links('https://bj.lianjia.com/zufang/')
for link in links:
    time.sleep(2)
    print('获取一个房子信息成功')
    house = get_house_info(link)
    insert(db, house)