mysqldb模块的简单用法
# - *- coding:utf-8-*-
import urllib2
import re import MySQLdb
import sys
reload(sys)
sys.setdefaultencoding('utf-8') class SQL(object):
conn=MySQLdb.connect(host="localhost",
port=3306,
user="root",
passwd="123",
db="test",
charset="utf8",) #转换成utf8若没有则会出来乱码
def insert(self,name,content): #函数的调用
cur=self.conn.cursor()
cur.execute("insert into xiaoshuo VALUES (NULL ,'%s','%s')"%(name,content)) #变量插入的形式 name是数据库表中的参数
cur.close()
self.conn.commit() mysql=SQL()#实例化类
User_Agent= 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0'
url="http://book.qidian.com/info/3608595"
headers={'User-Agent':User_Agent}
request=urllib2.Request(url,headers=headers)
htmll=urllib2.urlopen(request).read() reg=re.compile(r'<li data-rid=".*?"><a href="(.*?)" target="_blank" data-eid="qd_G55" data-cid=".*?" title=".*?">(.*?)</a>')
titles=re.finditer(reg,htmll)
for i in titles:
curl_=i.group(1)
name=i.group(2) print "正在爬取%s本"%name
htmlll=urllib2.urlopen('http:'+curl_).read()
regs=re.compile(r'<div class="read-content j_readContent">\s*([\s\S]*?)\s*</div>')
contents=re.findall(regs,htmlll) for m in contents:
content=m.replace('<p>','\r\n')
mysql.insert(name,content) #在数据库插入数据 name,content 和上边函数中的name不同,这是爬虫取到的name print "已完成%s"%name
mysql.conn.close() #关闭数据库 mysql数据库小知识点
创建表格
cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")
小说的内容应为text格式,不用填写len 最长的len是255 type(str)若字符长度小就用varchar
#插入一条数据
#cur.execute("insert into student values('2','Tom','3 year 2 class','9')") #修改查询条件的数据
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'") #删除查询条件的数据
#cur.execute("delete from student where age='9'")
mysqldb模块的简单用法的更多相关文章
- python制作简单excel统计报表2之操作excel的模块openpyxl简单用法
python制作简单excel统计报表2之操作excel的模块openpyxl简单用法 # coding=utf-8 from openpyxl import Workbook, load_workb ...
- Python中time和datetime模块的简单用法
python中与时间相关的一个模块是time模块,datetime模块可以看为是time模块的高级封装. time模块中经常用到的有一下几个方法: time()用来获取时间戳,表示的结果为从1970年 ...
- Python#常用的模块和简单用法
目录 random 随机模块 os 文件夹模块: time 时间模块: matplotlab.pyplot 作图模块 mpl_toolkits.mplot3d 绘制3D图模块 Pygame Reque ...
- python3中time模块与datetime模块的简单用法
__author__ = "JentZhang" import time # Timestamp 时间戳 print("Timestamp 时间戳:") pri ...
- datetime模块的简单用法
import datetime print(datetime.datetime.today()) #2018-08-14 14:18:28.575412 print(datetime.date.tod ...
- python optparse模块的简单用法
# coding = utf-8 from optparse import OptionParser from optparse import OptionGroup usage = 'Usage: ...
- python模块turtle简单用法
##初始化turtle:t = turtle.Turtle() 或者 t = turtle.Pen()##查询turtle状态#位置t.position() #箭头朝向,箭头朝向按照顺时针或者逆时针偏 ...
- python中MySQLdb模块用法实例
篇文章主要介绍了python中MySQLdb模块用法,以实例形式详细讲述了MySQLdb模块针对MySQL数据库的各种常见操作方法,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了python中 ...
- python timeit模块简单用法
timeit模块提供了一种简便的方法来为Python中的小块代码进行计时. 模块调用函数,stmp为要测试的函数,setup为测试环境,number为运行次数 timeit.timeit(stmt=) ...
随机推荐
- 去掉EditPlus自动备份bak文件
用EditPlus编辑文件是总是自动生成一个.bak文件. 其实想去掉EditPlus的自动备份也简单,方法如下: 打开菜单栏上的:工具->参数设置->文件 去掉“保存时创建备份文件”前的 ...
- Python笔记 001
#python版本:3.5.2 #for循环 for letter in ("xuyingke"): #默认循环 print ("当前字母:",letter) ...
- javascript的document.execCommand(转)
document.execCommand()方法处理html数据时常用语法格式如下: 代码: document.execCommand(sCommand[,交互方式, 动态参数]) 其 中:sComm ...
- python 模块openpyxl读excel文件
使用openpyxl模块来读取excel.要注意openpyxl读不再支持旧的xls格式. 先看一下操作前的excel是什么样子吧.对了,现在只支持xlsx格式的excel读取 我现在想在第三行插入3 ...
- 线下线上对接的一种思路(本地erp与线上电子商务平台对接)
目前很多公司都希望本地的ERP能够与线上的电子商务平台进行对接. 但是很多的线下ERP系统商不愿意修改代码来做相应的对接,或者觉得太话费成本. 而对于企业本身,又会有很多的特殊需求. 下面略述一家进口 ...
- 推荐csdn里的几篇activiti基础入门及提高的博客
昨天有个网友加qq询问我有没有非maven搭建的activiti项目的demo,因为我博客中写了一个用maven,我当时没有,于是晚上回家尝试了一下,结果比较容易就实现了. 之后和那个网友聊了一下,他 ...
- 封装实现一个自己的tabbar
实现效果:
- Hbase条件筛选
需求来自于,模糊查找当天的所有记录,并查找对应列的记录数 public static void main(String[] args) throws Exception{ //创建HBase连接 Co ...
- 【转】Cordova文件传输插件fileTransfer
任务要求: 访问手机的目录,选择一个文件,并使用该插件将指定文件传输到远程主机的某个指定目录中. HTML <!DOCTYPE html> <!-- Licensed to the ...
- 便捷的php操作mysql库MysqliDb
github 地址:https://github.com/joshcam/PHP-MySQLi-Database-Class MysqliDb -- Simple MySQLi wrapper and ...