【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 ...
随机推荐
- SQLSERVER误删Windows登录用户验证方式使用Windows身份验证的解决方法
今天看到这篇文章:没有了SA密码,无法Windows集成身份登录,DBA怎么办? 想起来之前着急哥问我的一个问题,一个DBA删除了Windows登录用户,而且SQLSERVER服务器的验证方式是Win ...
- javascript 编辑网页
javascript:document.body.contentEditable='true';document.designMode='on'; void 0 出处:http://zhidao.ba ...
- mac 设置 代理 上网 步骤 同时设置邮件代理
设置mac电脑网络代理步骤如下: 第一步:打开网络偏好设置 第二步:依次输入如下参数 第三步:设置代理服务器地址 设置邮箱使用代理步骤如下: 第一步: 第二步:选择使用“系统代理设置”即可,无需其它多 ...
- ASP.NET :Virtual Application vs Virtual Directory
原文地址:http://blogs.msdn.com/b/wenlong/archive/2006/11/22/virtual-application-vs-virtual-directory.asp ...
- 架构:The Onion Architecture : part 1(洋葱架构:第一篇)(转载)
原文地址:http://jeffreypalermo.com/blog/the-onion-architecture-part-1/. I've spoken several times about ...
- 裂痕第一至五季/以法之名Damages迅雷下载
本季第一至五季Damages Season (2007-2012)看点:<裂痕>又是一部以法律剧情为主打,其间又掺杂着悬念,阴谋,破案等因素的剧集.女主角帕蒂-赫韦斯(Patty Hewe ...
- 从源码角度一步一步来修改PreferenceActivity界面
PreferenceActivity给我们封装好了一个数据存储对象,我们只需要在xml文件中写上控件即可完成简单的设置界面.但是系统提供的设置界面十分的简陋,要想做的好看必须要自己来进行修改 ...
- 选中TreeView的某节点,并加背景颜色
一:按钮事件,遍历所有节点 private void button2_Click(object sender, EventArgs e) { foreach (TreeNode n in TreeVi ...
- ExpandoObject对象的JSON序列化
如果: dynamic expando = new ExpandoObject(); d.SomeProp=SomeValueOrClass; 然后,我们在控制器中: return new JsonR ...
- MongoDB 分布式部署教程
本文将介绍如何使用 MongoDB 提供的 Replica Set 和 Shards 功能构建一个分布式 MongoDB 集群. Replica Set 部署 我们先从部署一个三节点的 Replica ...