【python】insertDB1.02
#------------------------------------------------------------------------------------
# insertDB1.02,读取理想论坛爬虫生成的数据,然后写入DB
# 2018年5月7日
#------------------------------------------------------------------------------------
import pymysql
import time
import datetime
import os
import json
#------------------------------------
# 取得当前时间
#------------------------------------
def currTime():
currTime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
return currTime
#------------------------------------
# 入口函数
#------------------------------------
def main(folder):
starttime = datetime.datetime.now()
# 预制查找数组
arrSize=5962;
arr=[]
for i in range(0,arrSize):
arr.append(0)
# 打开目录从文件里取数据
allinfos=[]
for filename in os.listdir(folder):
filePathname=folder+"/"+filename
with open(filePathname,'r',encoding='utf-8') as fObj:
infos=json.load(fObj)
allinfos.extend(infos)
arrTmp=filename.split('.')
index=int(arrTmp[0])
arr[index]=1
# 看哪些位置未填上,未填上的就是缺失文件处
for i in range(0,arrSize):
if arr[i]==0:
print("位置为"+str(i)+"的文件缺失")
print("拟向数据库插入"+str(len(allinfos))+"条记录")
# 打开数据库并插值
conn=pymysql.connect(host=',db='test',charset='utf8')
cur=conn.cursor();
sum=0
for info in allinfos:
try:
arr=[info['楼层'],info['作者'],info['日期'],info['时间'],currTime(),info['url'],info['title'],info['内容']]
count=cur.execute('insert into test.topic17(floor,author,tdate,ttime,addtime,url,title,content) values (%s,%s,%s,%s,%s,%s,%s,%s)',arr)
sum+=count
except Exception as e:
print("出现异常:"+str(e)+",此时info="+str(info))
continue;
conn.commit()
conn.close()
print("已向数据库插入"+str(sum)+"条记录")
# 计算用时
endtime = datetime.datetime.now()
print("插数据用时"+str((endtime - starttime).seconds)+"秒")
# 开始
main("./15_38_48")
输出:
C:\Users\horn1\Desktop\python\33>python insertDB.py
位置为1558的文件缺失
位置为1608的文件缺失
位置为1758的文件缺失
位置为1763的文件缺失
位置为1839的文件缺失
位置为1840的文件缺失
位置为1875的文件缺失
位置为1995的文件缺失
位置为2008的文件缺失
位置为2009的文件缺失
位置为2012的文件缺失
位置为2024的文件缺失
位置为2025的文件缺失
位置为2026的文件缺失
位置为2030的文件缺失
拟向数据库插入115149条记录
出现异常:'utf-8' codec can't encode character '\ud83d' in position 275: surrogates not allowed,此时info={'楼层': '7283楼', '作者': '爱丽说', '日期': '2014-10-22', '时间': '11:33', 'title': ' 拥抱阳光龙理论2018成功的路上并不拥挤,我们一起迈步前行,找到好老师就有好方法! ', 'url': 'http://www.55188.com/thread-5673944-365-2.html', '内容': '倒霉的我,1号没买到,买 了2-3号,宝箱更新太不及时,强烈要求老师微信同步\ud83d😓😓😓😓😓😓😭😭'}
出现异常:'utf-8' codec can't encode character '\ud83d' in position 275: surrogates not allowed,此时info={'楼层': '7285楼', '作者': '爱丽说', '日期': '2014-10-22', '时间': '11:37', 'title': ' 拥抱阳光龙理论2018成功的路上并不拥挤,我们一起迈步前行,找到好老师就有好方法! ', 'url': 'http://www.55188.com/thread-5673944-365-2.html', '内容': '倒霉的我,1号没买到,买 了2-3号,宝箱更新太不及时,强烈要求老师微信同步\ud83d😓😓😓😓😓😓😭😭'}
已向数据库插入115147条记录
插数据用时86秒
C:\Users\horn1\Desktop\python\33>
检查结果比较一下,Python爬虫的文件丢失率为15/5961=0.25%,而Nodejs爬虫的损失率高达10%~20%,天壤之别!
2018年5月7日13点40分
【python】insertDB1.02的更多相关文章
- 【Python】torrentParser1.02
#------------------------------------------------------------------------------------ # torrentParse ...
- 【Python】 零碎知识积累 II
[Python] 零碎知识积累 II ■ 函数的参数默认值在函数定义时确定并保存在内存中,调用函数时不会在内存中新开辟一块空间然后用参数默认值重新赋值,而是单纯地引用这个参数原来的地址.这就带来了一个 ...
- 【python】列出http://www.cnblogs.com/xiandedanteng中所有博文的标题
代码: # 列出http://www.cnblogs.com/xiandedanteng中所有博文的标题 from bs4 import BeautifulSoup import requests u ...
- 【python】redis基本命令和基本用法详解
[python]redis基本命令和基本用法详解 来自http://www.cnblogs.com/wangtp/p/5636872.html 1.redis连接 redis-py提供两个类Redis ...
- 【Python②】python之首秀
第一个python程序 再次说明:后面所有代码均为Python 3.3.2版本(运行环境:Windows7)编写. 安装配置好python后,我们先来写第一个python程序.打开IDLE (P ...
- 【python】多进程锁multiprocess.Lock
[python]多进程锁multiprocess.Lock 2013-09-13 13:48 11613人阅读 评论(2) 收藏 举报 分类: Python(38) 同步的方法基本与多线程相同. ...
- 【python】SQLAlchemy
来源:廖雪峰 对比:[python]在python中调用mysql 注意连接数据库方式和数据操作方式! 今天发现了个处理数据库的好东西:SQLAlchemy 一般python处理mysql之类的数据库 ...
- 【python】getopt使用
来源:http://blog.chinaunix.net/uid-21566578-id-438233.html 注意对比:[python]argparse模块 作者:limodou版权所有limod ...
- 【Python】如何安装easy_install?
[Python]如何安装easy_install? http://jingyan.baidu.com/article/b907e627e78fe146e7891c25.html easy_instal ...
随机推荐
- USB设备的插入和弹出的监听以及软弹出可移动媒体(如Windows的移除USB设备) .
一.监听USB设备的插入和弹出 当USB设备插入或者弹出时,Windows会产生一条全局消息:WM_DEVICECHANGE 我们需要做的是,获得这条消息的wParam参数,如果为DBT_DEVICE ...
- Parallel Programming--perfbook
https://www.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html
- Revit MEP API找到连接器连接的连接器
通过conn.AllRefs;可以找到与之连接的连接器. //连接器连接的连接器 [TransactionAttribute(Autodesk.Revit.Attributes.Transaction ...
- Redis源代码分析(三十五)--- redis.c服务端的实现分析(2)
在Redis服务端的代码量真的是比較大,假设一个一个API的学习怎么实现,无疑是一种效率非常低的做法,所以我今天对服务端的实现代码的学习,重在他的运行流程上.而对于他的模块设计在上一篇中我已经分析过了 ...
- C#各种泛型集合体验
本篇体验除Queue<T>和Stack<T>之外的其它泛型集合. SortedList<TKey, TValue> SortedList<TKey, TVal ...
- XE5 修复 安卓 输入法隐藏 后 无法退出的问题 3.1
(****************************************************)(* *)(* 编写:爱吃猪头肉 & Flying Wang *)(* 上面的版权声 ...
- poi excel导入导出
pom <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artif ...
- IOS NSLog 打印bool值
输出BOOL值的方法:NSLog(@"%@",YES?@"YES":@"NO");%@输出字符串. NSLog(@"ifReadO ...
- eclipse 中 import sun.misc.BASE64Decoder; 报错
from://http://blog.sina.com.cn/s/blog_48964b120101ahrf.html 在android做3DES加密功能时 eclipse 中 import sun. ...
- Oracle中删除用户下所有对象的多种方法
Oracle删除用户下所有对象的方法未必人人都会,下面就为您介绍两种常用的Oracle删除用户下所有对象的方法,希望对您学习Oracle删除用户方面能有所帮助. 方法1: drop user XX ...