数据存储之使用mysql数据库存储数据
推荐安装mysql5.7环境:
- 官网下载:https://dev.mysql.com/downloads/installer/5.7.html
- 如果提示没有
.NET Framework框架。那么就在提示框中找到下载链接,下载一个就可以了。 - 如果提示没有
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。必须要有一个中间件,或者叫做驱动程序。驱动程序有很多。比如有mysqldb、mysqlclient、pymysql等。在这里,我们选择用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*方法。
fetchone():这个方法每次之获取一条数据。fetchall():这个方法接收全部的返回结果。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数据库存储数据的更多相关文章
- CentOS6 更改Mysql数据库的数据存放位置
mysql使用yum安装时,默认的数据是存储在/var/lib/mysql下.一般情况下,为了数据的安全性,建议将mysql数据库的数据文件存储在系统的第二块磁盘上的目录下可以按照以下步骤进行操作: ...
- MYSQL——数据库存储引擎!
本人安装mysql版本为:mysql Ver 14.14 Distrib 5.7.18, for Win64 (x86_64),查看mysql的版本号方式:cmd-->mysql --vers ...
- Mysql数据库写入数据速度优化
Mysql数据库写入数据速度优化 1)innodb_flush_log_at_trx_commit 默认值为1:设置为0,可以提高写入速度. 值为0:提升写入速度,但是安全方面较差,mysql服务器 ...
- 一步一步跟我学习hadoop(7)----hadoop连接mysql数据库运行数据读写数据库操作
为了方便 MapReduce 直接訪问关系型数据库(Mysql,Oracle).Hadoop提供了DBInputFormat和DBOutputFormat两个类.通过DBInputFormat ...
- 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 ...
- 修改mysql数据库存储路径
最近一段比较忙,所以一直没有及时的更新总结一下测试路上遇到的问题,今天先来分享一下如何修改mysql存储路径(场景:在自己电脑上搭建的服务器上安装mysql,二.在公司自己的服务器上搭建mysql数据 ...
- 用Python向MySQL数据库插入数据
最近一直在学习MySQL数据库,很感兴趣.这次我做了一个简单的尝试,使用Python3.4与MySQL数据库进行交互,将一份从雪球网上下载的某股票数据上传至MySQL数据库.仅为初学者提供参考,高手请 ...
- mysql数据库delete数据时不支持表别名
今天在帮同事查看一条删除的SQL语句执行出错的问题 SQL语句如下: 1 DELETE FROM LEAD_SYSTEM_MENU_ORG_REF as t WHERE t.resourceid='4 ...
- 转】mysql数据库delete数据时不支持表别名
原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4012853.html 感谢! 今天在帮同事查看一条删除的SQL语句执行出错的问题 SQL语句如下: 1 DELE ...
随机推荐
- 4 - Channelhandler和ChannelPipeline
4.1 Channelhandler 4.1.1 Channel声明周期(状态事件) 方法 描述 ChannelUnregistered Channnel已创建,但是未注册到EventLoop Cha ...
- 一般的linux系统默认安装的vim是精简版
一般的linux系统默认安装的vim是精简版(vim-tiny),所以不能配置语法检查等属性或获取在线帮助.需要安装vim-x:x.x.x,vim-common,vim-runtime. :synta ...
- .bak文件数据还原
.bak文件还原(见下图) 1.连接上数据库,右键数据库,选择新建数据库,输入你要还原数据库的名称 2.数据库右键-->任务-->还原-->数据库,弹出窗口选择[源设备],选择.ba ...
- vue 导出excel
1.安装三个依赖包 npm install -S file-saver npm install -S xlsx npm install -D script-loader 2.在项目中创建一个文件夹(比 ...
- Employees Earning More Than Their Managers
The Employee table holds all employees including their managers. Every employee has an Id, and there ...
- OpenStack Ocata Telemetry 警告服务部署
下列操作在控制节点上进行: 1 准备条件 在配置OpenStack Telemetry服务之前,你必须创建数据库.服务凭证和API端点. 1.1 数据库 以root用户连接数据库服务器,创建glanc ...
- 在Ubuntu中安装MySQL
在Ubuntu中安装MySQL Ubuntu实用工具系列文章,将介绍基于Linux ubuntu的各种工具软件的配置和使用.有些工具大家早已耳熟能详,有些工具经常用到但确依然陌生.我将记录我在使用操作 ...
- ubuntu双屏调整分辨率
查看屏幕硬件指标 # xrandr Screen 0: minimum 8 x 8, current 2390 x 768, maximum 32767 x 32767 LVDS1 connected ...
- pat甲级1020中序后序求层序
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- Head First HTML与CSS阅读笔记(一)
之前写过不少前端界面,但是没有完整阅读过一本HTML与CSS的书籍,都是用到什么查什么,最近闲暇之余想巩固加深一下前端基础方面的知识,阅读了<Head First HTML与CSS>,感觉 ...