连接数据库:

conn=pymysql.connect(host='*.*.*.*',port=,user='root',password='',charset='utf8')

建立游标:

cur = conn.cursor()

创建一个名字叫 lj 的数据库:

cur.execute('create database if not EXISTS lj character set utf8;') #用cur.execute执行SQL语句  注意如果要插入中文数据,建库时必须要加 character set utf8 意思是把字符集设置为utf8,要不然中文不能显示!

选则要操作的数据库:

conn.select_db('lj')

添加7个字段:

cur.execute('create table JiNan(source VARCHAR(50),addr VARCHAR(50),stru VARCHAR(50),dir_1 VARCHAR(50),floor VARCHAR(50),size VARCHAR(50),money VARCHAR(50));')

提交修改:

conn.commit()

向表中插入数据:

cur.execute("insert into JiNan(source,addr,stru,dir_1,floor,size,money) VALUES('%s','%s','%s','%s','%s','%s','%s')"%(so,ad,st,di,fl,si,mn))

整个爬取网站数据然后存入数据库的脚本:

#-*- coding:utf- -*-
import requests,re,pymysql
def get_info(url):
html_response = requests.get(url)
html_response.encoding = 'utf-8'
add_add = r'<span class="region">(.*?)&nbsp;&nbsp;'
stru_add = r'class="zone"><span>(.*?)&nbsp;&nbsp;'
size_add = r'<span class="meters">(.*?)&nbsp;&nbsp;'
dir_add = r'</span><span>(.*?)</span></div><div'
floor_add = r'<span>/</span>(.*?)</div></div><div'
source_add = r'<div class="con"><a href=".*?">(.*?)</a>'
money_add = r'lass="price"><span class="num">(.*?)</span>元/月' add = re.findall(add_add,html_response.text)
stru = re.findall(stru_add,html_response.text)
size = re.findall(size_add,html_response.text)
dir_1 = re.findall(dir_add,html_response.text)
floor = re.findall(floor_add,html_response.text)
source = re.findall(source_add,html_response.text)
money = re.findall(money_add,html_response.text) #print(add[],stru[],dir_1[],floor[],source[],size[],money[])
return source,add,stru,dir_1,floor,size,money
def into_db(source,add,stru,dir_1,floor,size,money,cur,conn):
for so,ad,st,di,fl,si,mn in zip(source,add,stru,dir_1,floor,size,money):
cur.execute("insert into JiNan(source,addr,stru,dir_1,floor,size,money) VALUES('%s','%s','%s','%s','%s','%s','%s')"%(so,ad,st,di,fl,si,mn))
conn.commit() if __name__ == '__main__': conn = pymysql.connect(host='*.*.*.*',port=,user='root',password='',charset='utf8')
cur = conn.cursor()
cur.execute('create database if not EXISTS lj character set utf8;')
conn.select_db('lj')
cur.execute('create table JiNan(source VARCHAR(50),addr VARCHAR(50),stru VARCHAR(50),dir_1 VARCHAR(50),floor VARCHAR(50),size VARCHAR(50),money VARCHAR(50));')
conn.commit()
for i in range(,):
url = 'https://jn.lianjia.com/zufang/pg' + str(i)
print(url)
source,add,stru,dir_1,floor,size,money = get_info(url)
into_db(source,add,stru,dir_1,floor,size,money,cur,conn)
print('OK... ...')
conn.close()

Python_PyMySQL数据库操作的更多相关文章

  1. 如何在高并发环境下设计出无锁的数据库操作(Java版本)

    一个在线2k的游戏,每秒钟并发都吓死人.传统的hibernate直接插库基本上是不可行的.我就一步步推导出一个无锁的数据库操作. 1. 并发中如何无锁. 一个很简单的思路,把并发转化成为单线程.Jav ...

  2. 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~

    最近用php做了点小东东,用上了ezSQL,感觉真的很ez,所以拿来跟大家分享一下~ ezSQL是一个非常好用的PHP数据库操作类.著名的开源博客WordPress的数据库操作就使用了ezSQL的My ...

  3. MySQL 系列(二) 你不知道的数据库操作

    第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 本章内容: 查看\创建\使用\删除 数据库 用户管理及授权实战 局域网 ...

  4. ABP创建数据库操作步骤

    1 ABP创建数据库操作步骤 1.1 SimpleTaskSystem.Web项目中的Web.config文件修改数据库配置. <add name="Default" pro ...

  5. 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)

    目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...

  6. django数据库操作和中间件

    数据库配置 django的数据库相关表配置在models.py文件中,数据库的连接相关信息配置在settings.py中 models.py相关相关参数配置 from django.db import ...

  7. [Android Pro] 完美Android Cursor使用例子(Android数据库操作)

    reference to : http://www.ablanxue.com/prone_10575_1.html 完美 Android Cursor使用例子(Android数据库操作),Androi ...

  8. phpcms v9 中的数据库操作函数

    1.查询 $this->select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='')   返回 ...

  9. Android打造属于自己的数据库操作类。

    1.概述 开发Android的同学都知道sdk已经为我们提供了一个SQLiteOpenHelper类来创建和管理SQLite数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要 ...

随机推荐

  1. HTML5中 基本用法及属性 韩俊强的博客

    从今天开始更新H5相关学习:希望大家能一起学习,多学习一门语言,多一门乐趣! 了解Html5: Html5基本属性: <!DOCTYPE html> <html lang=" ...

  2. Bootstrap简单HelloWorld入门教程

    这篇教程旨在让你在20分钟内学会使用twitter bootstrap创建一个站点.看完这个教程后你应该能够使用bootstrap来建立一个基本的响应式布局的页面,了解栅格系统,并且能够使用boots ...

  3. How to Find the Self Service Related File Location and Versions

     How to Find the Self Service Related File Location and Versions (文档 ID 781385.1) In this Document ...

  4. 就这么 来ADO.net类操作数据库

    使用ADO.net操作数据库其实也是很简单,而且使用频率蛮高的一种方式.话不多说,上代码才更容易理解. 首先,先要引入数据库操作相关的命名空间,这样才能使用下面的代码 //数据库连接引用的命名空间 u ...

  5. ROC曲线的AUC(以及其他评价指标的简介)知识整理

    相关评价指标在这片文章里有很好介绍 信息检索(IR)的评价指标介绍 - 准确率.召回率.F1.mAP.ROC.AUC:http://blog.csdn.net/marising/article/det ...

  6. 网站开发进阶(三十五)JSP页面中的pageEncoding和contentType两种属性

    JSP页面中的pageEncoding和contentType两种属性 本文介绍了在JSP页面中经常用的两种属性,分别是pageEncoding和contentType,希望对你有帮助,一起来看. 关 ...

  7. 关于Tomcat的URIEncoding以及GET乱码

    最近在维护着Linux上的服务器,当然,开发和前期测试是在windows上执行的. 在做意见反馈的时候,出现了windows上正常,Linux下却是乱码的问题. 先排查了web服务器与mysql的连接 ...

  8. HTML5 classList API接口

    原文地址:HTML5 classList API 原文日期: 2010年07月13日 翻译日期: 2013年08月23日 当我陷入JavaScrip和JavaScript类库框架之中时,我总是有种希望 ...

  9. Redis简介 Linux安装Redis Redis使用

    其他一些操作(包括 APPEND.GETRANGE.MSET 和 STRLENGTH 也可用于字符串.请参见http://doc.redisfans.com/string/index.html ) 使 ...

  10. (NO.00001)iOS游戏SpeedBoy Lite成形记(二十四)

    我们回到Xcode,打开GameScene.m,首先要添加实例变量: CCNode *_trackLine; 为了根据选中的赛道更新_trackLine的位置,我们添加一个显示方法: -(void)s ...