【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 ...
随机推荐
- MSDN WinUSB Example
The WinUSB user-mode library uses device interface classes to communicate with the kernel-mode USB s ...
- Revit API创建墙的保温层修改墙厚度
start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] / ; ; ...
- Xamarin adventures – Differences between iOS simulator and device
I had been happily coding an iOS app (targeting iPad) using Xamarin/VS.Net with everything working f ...
- NSZombie 详解 -僵尸对象
1.什么是僵尸对象? 简而言之,就是过度释放的对象. 2.僵尸对象有什么特点? 如果一个对象a被变成了僵尸对象,那么,在进行下面的判断时,a是会被系统当成一个对象来进行判断的.但是,如果你使用a进行其 ...
- 2018房地产沉思录 z
在中国,房价问题几乎有一个铁律:越调控越暴涨. 刚刚进入5月,全国各地发布的调控政策数量就已经超过了115个.仅4月份,全国各种房地产调控政策合计多达33次,25个城市与部门发布调控政策,其中海南.北 ...
- C语言之基本算法24—黄金切割法求方程近似根
//黄金切割法! /* ================================================================ 题目:用黄金切割法求解3*x*x*x-2*x* ...
- 禁止tomcat生成catalina.out、localhost_access_log、host-manager.log、localhost.log、manager.log这些文件
1.找到tomcat目录conf,logging.properties把这个文件重命名(推荐:也可以根据自己的需要更改里面的配置文件) 其实删掉也可以但是不建义 这样就不会生成host-manager ...
- YAML 语言教程
编程免不了要写配置文件,怎么写配置也是一门学问. YAML 是专门用来写配置文件的语言,非常简洁和强大,远比 JSON 格式方便. 本文介绍 YAML 的语法,以 JS-YAML 的实现为例.你可以去 ...
- 对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾
今天在springmvc集成mybatis时,遇到一个错误 "characterEncoding" 的引用必须以 ';' 分隔符结尾. 这是“&”定义与解析的原因,需要对& ...
- bash 设置
export COLUMNS=500 $* 将所有命令行参数当做单个文本值包含 $@ 将所有命令行参数当做独立的文本值包含 $# 命令行参数数目 $? 最近使用的前端进程的退出状态码 $- 当前命令选 ...