推荐安装mysql5.7环境:

  1. 官网下载:https://dev.mysql.com/downloads/installer/5.7.html
  2. 如果提示没有.NET Framework框架。那么就在提示框中找到下载链接,下载一个就可以了。
  3. 如果提示没有Microsoft Virtual C++ x64(x86),那么百度或者谷歌这个软件安装即可。

Navicat Premium 版本:

navicat是一个操作mysql数据库非常方便的软件。使用他操作数据库,就跟使用excel操作数据是一样的。

1.官网下载:http://www.navicat.com.cn/download/navicat-premium

推荐使用 (官网最新版的没有破解成功,推荐这个版本)Navicat Premium 12.0.27简体中文64位,密码: s9f8

2.破解参考
https://blog.csdn.net/pippa134679/article/details/81354131
https://www.jianshu.com/p/5f693b4c9468

安装驱动程序:

Python要想操作MySQL。必须要有一个中间件,或者叫做驱动程序。驱动程序有很多。比如有mysqldbmysqlclientpymysql等。在这里,我们选择用pymysql。安装方式也是非常简单,通过命令pip install pymysql即可安装。

数据库连接:

数据库连接之前。首先先确认以下工作完成,这里我们以一个pymysql_test数据库.以下将介绍连接mysql的示例代码:

import pymysql

    db = pymysql.connect(
host="127.0.0.1",
user='root',
password='root',
database='pymysql_test',
port=3306
)
cursor = db.cursor()
cursor.execute("select 1")
data = cursor.fetchone()
print(data)
db.close()

插入数据:

import pymysql

db = pymysql.connect(
host="127.0.0.1",
user='root',
password='root',
database='pymysql_test',
port=3306
)
cursor = db.cursor()
sql = """
insert into user(
id,username,gender,age,password
)
values(null,'abc',1,18,'111111');
"""
cursor.execute(sql)
db.commit()
db.close()

如果在数据还不能保证的情况下,可以使用以下方式来插入数据:

sql = """
insert into user(
id,username,gender,age,password
)
values(null,%s,%s,%s,%s);
""" cursor.execute(sql,('spider',1,20,'222222'))

查找数据:

使用pymysql查询数据。可以使用fetch*方法。

    1. fetchone():这个方法每次之获取一条数据。
    2. fetchall():这个方法接收全部的返回结果。      
    3. fetchmany(size):可以获取指定条数的数据。

  示例代码如下:

cursor = db.cursor()

sql = """
select * from user
""" cursor.execute(sql)
while True:
result = cursor.fetchone()
if not result:
break
print(result)
db.close()

或者是直接使用fetchall,一次性可以把所有满足条件的数据都取出来:

cursor = db.cursor()

sql = """
select * from user
""" cursor.execute(sql)
results = cursor.fetchall()
for result in results:
print(result)
db.close()

或者是使用fetchmany,指定获取多少条数据:

cursor = db.cursor()

sql = """
select * from user
""" cursor.execute(sql)
results = cursor.fetchmany(1)
for result in results:
print(result)
db.close()

删除数据:

cursor = db.cursor()

sql = """
delete from user where id=1
""" cursor.execute(sql)
db.commit()
db.close()

更新数据:

conn = pymysql.connect(host='localhost',user='root',password='root',database='pymysql_demo',port=3306)
cursor = conn.cursor() sql = """
update user set username='aaa' where id=1
"""
cursor.execute(sql)
conn.commit() conn.close()

实战抓取安居客广西南宁全区的租房信息(正则表达式,MySQL数据库保存)

import requests
import re
import pymysql # 实战抓取安居客广西南宁全区的租房信息(正则表达式,数据库保存)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
} def insert_house_detail(url):
print(url)
# 连接数据库和添加数据
conn = pymysql.connect(host='localhost', user='root', password='password', database='zufang', port=3306)
cursor = conn.cursor() # 添加数据
sql = """
insert into house(id,title,img,price,payType,leaseType,houseType,address,detail) values(null,%s,%s,%s,%s,%s,%s,%s,%s)
""" # 获取数据并添加到数据库
response = requests.get(url, headers=headers)
text = response.text
title = re.findall(r'<h3\sclass="house-title">(.*?)</h3>', text, re.DOTALL)[0]
img = re.findall(r'<div\sclass="img_wrap">.*?<img\sdata-src="(.*?)".*?>', text, re.DOTALL)[0]
price = re.findall(r'<span\sclass="price">.*?<em>(.*?)</em>', text, re.DOTALL)[0]
payType = re.findall(r'<span\sclass="type">(.*?)</span>', text, re.DOTALL)[0]
leaseType = re.findall(r'<span\sclass="info">(.*?)</span>', text, re.DOTALL)[1]
houseType = re.findall(r'<span\sclass="info">(.*?)</span>', text, re.DOTALL)[0]
# houseType = re.findall(r'<ul.*?class="f14">.*?<span\sclass="c_888 mr_15">.*?<span>(.*?)</span>.*?</li>', text, re.DOTALL)[0].replace(' ', '').replace(' ', '').strip() address = re.findall(r'<li\sclass="house-info-item l-width">.*?<a.*?>(.*?)</a>', text, re.DOTALL)
detail_tag = re.findall(r'<div\sclass="auto-general">(.*?)</div>', text, re.DOTALL)[0]
# 去掉抓取到标签和空格
detail = re.sub('<.+?>', "", detail_tag).replace(' ', '').strip() cursor.execute(sql, (title, img, price, payType, leaseType, houseType, address, detail))
conn.commit()
conn.close() def parse_page(url): response = requests.get(url, headers=headers)
text = response.text
# 先获取url
urls = re.findall(r'<div\sclass="zu-info">.*?<a.*?href="(.*?)".*?>.*?</a>', text, re.DOTALL)[1:-2] for index,url_tag in enumerate(urls):
insert_house_detail(url_tag) def main():
for x in range(1,21):
url = 'https://nn.zu.anjuke.com/fangyuan/p%s/' % x
parse_page(url) if __name__ == '__main__':
main()

  

数据存储之使用mysql数据库存储数据的更多相关文章

  1. CentOS6 更改Mysql数据库的数据存放位置

    mysql使用yum安装时,默认的数据是存储在/var/lib/mysql下.一般情况下,为了数据的安全性,建议将mysql数据库的数据文件存储在系统的第二块磁盘上的目录下可以按照以下步骤进行操作: ...

  2. MYSQL——数据库存储引擎!

    本人安装mysql版本为:mysql  Ver 14.14 Distrib 5.7.18, for Win64 (x86_64),查看mysql的版本号方式:cmd-->mysql --vers ...

  3. Mysql数据库写入数据速度优化

    Mysql数据库写入数据速度优化 1)innodb_flush_log_at_trx_commit 默认值为1:设置为0,可以提高写入速度.  值为0:提升写入速度,但是安全方面较差,mysql服务器 ...

  4. 一步一步跟我学习hadoop(7)----hadoop连接mysql数据库运行数据读写数据库操作

        为了方便 MapReduce 直接訪问关系型数据库(Mysql,Oracle).Hadoop提供了DBInputFormat和DBOutputFormat两个类.通过DBInputFormat ...

  5. MySQL数据库插入数据出现 ERROR 1526 (HY000): Table has no partition for value xxx

    MySQL数据库插入数据出现ERROR 1526 (HY000): Table has no partition for value xxx工作的时候发现无法插入数据,报错:ERROR 1526 (H ...

  6. 修改mysql数据库存储路径

    最近一段比较忙,所以一直没有及时的更新总结一下测试路上遇到的问题,今天先来分享一下如何修改mysql存储路径(场景:在自己电脑上搭建的服务器上安装mysql,二.在公司自己的服务器上搭建mysql数据 ...

  7. 用Python向MySQL数据库插入数据

    最近一直在学习MySQL数据库,很感兴趣.这次我做了一个简单的尝试,使用Python3.4与MySQL数据库进行交互,将一份从雪球网上下载的某股票数据上传至MySQL数据库.仅为初学者提供参考,高手请 ...

  8. mysql数据库delete数据时不支持表别名

    今天在帮同事查看一条删除的SQL语句执行出错的问题 SQL语句如下: 1 DELETE FROM LEAD_SYSTEM_MENU_ORG_REF as t WHERE t.resourceid='4 ...

  9. 转】mysql数据库delete数据时不支持表别名

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4012853.html 感谢! 今天在帮同事查看一条删除的SQL语句执行出错的问题 SQL语句如下: 1 DELE ...

随机推荐

  1. 海量日志收集利器 —— Flume

    Flume 是什么? Flume是一个分布式.可靠.和高可用的海量日志聚合的系统,支持在系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并写到各种数据接受方(可定制)的 ...

  2. FIRST集和FOLLOW集,FIRSTVT集和LASTVT集的求法

    学习编译原理时, 这几个集合相信大家并不陌生:FIRST.FOLLOW.FIRSTVT.LASTVT. 其中First和Follow是一对,而Firstvt和Lastvt是一对. 它们的作用分别是: ...

  3. 还不知道如何使用 IDEA ?教你三招快速掌握 IDEA

    前言 IntelliJ IDEA 是一个非常强大的 IDE,拥有许多功能.在 IDEA 中大部分功能都可以用快捷键去完成,如果掌握了大部分快捷键,可以只使用键盘开发了. ps: 最近正在练习快捷键,准 ...

  4. pat1059. Prime Factors (25)

    1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...

  5. Windows安全认证是如何进行的?[Kerberos篇]

    最近一段时间都在折腾安全(Security)方面的东西,比如Windows认证.非对称加密.数字证书.数字签名.TLS/SSL.WS-Security等.如果时间允许,我很乐意写一系列的文章与广大网友 ...

  6. pygame 使用

    模块概况 display image event key mouse font 类概况 Rect: 返回的矩阵区域(图片) Surface: 可以看做是一个贴图, 它就是来显示的 display(与显 ...

  7. JAVA 集合类小结

    一 集合和数组 因为本人也是个go的爱好者,所以对于集合类算是摸的比较透的. 说到集合,必须了解数组和集合. Java的数组长度固定,集合长度不定.集合是特定的数据结构的集合. 而go里面并没有集合, ...

  8. Counting blessings can actually increase happiness and health by reminding us of the good things in life.

    Counting blessings can actually increase happiness and health by reminding us of the good things in ...

  9. Spring Boot概要

    1.Spring Boot使用“习惯优于配置”(项目中存在大量的配置,此外还内置了一个习惯性的配置)的理念,使用户的项目实现快速运行.通过学习Spring Boot中的配置文件application. ...

  10. 手机端@media screen布局自适应

    @media only screen and (min-width: 310px) and (max-width: 360px) { }@media only screen and (min-widt ...