#------------------------------------------------------------------------------------
# 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的更多相关文章

  1. 【Python】torrentParser1.02

    #------------------------------------------------------------------------------------ # torrentParse ...

  2. 【Python】 零碎知识积累 II

    [Python] 零碎知识积累 II ■ 函数的参数默认值在函数定义时确定并保存在内存中,调用函数时不会在内存中新开辟一块空间然后用参数默认值重新赋值,而是单纯地引用这个参数原来的地址.这就带来了一个 ...

  3. 【python】列出http://www.cnblogs.com/xiandedanteng中所有博文的标题

    代码: # 列出http://www.cnblogs.com/xiandedanteng中所有博文的标题 from bs4 import BeautifulSoup import requests u ...

  4. 【python】redis基本命令和基本用法详解

    [python]redis基本命令和基本用法详解 来自http://www.cnblogs.com/wangtp/p/5636872.html 1.redis连接 redis-py提供两个类Redis ...

  5. 【Python②】python之首秀

       第一个python程序 再次说明:后面所有代码均为Python 3.3.2版本(运行环境:Windows7)编写. 安装配置好python后,我们先来写第一个python程序.打开IDLE (P ...

  6. 【python】多进程锁multiprocess.Lock

    [python]多进程锁multiprocess.Lock 2013-09-13 13:48 11613人阅读 评论(2) 收藏 举报  分类: Python(38)  同步的方法基本与多线程相同. ...

  7. 【python】SQLAlchemy

    来源:廖雪峰 对比:[python]在python中调用mysql 注意连接数据库方式和数据操作方式! 今天发现了个处理数据库的好东西:SQLAlchemy 一般python处理mysql之类的数据库 ...

  8. 【python】getopt使用

    来源:http://blog.chinaunix.net/uid-21566578-id-438233.html 注意对比:[python]argparse模块 作者:limodou版权所有limod ...

  9. 【Python】如何安装easy_install?

    [Python]如何安装easy_install? http://jingyan.baidu.com/article/b907e627e78fe146e7891c25.html easy_instal ...

随机推荐

  1. CAS服务器配置

    参考文献: http://sucre.blog.51cto.com/1084905/683624 1.安装部署CAS Server 从官网下载CAS Server,今天发现CAS Server的官网居 ...

  2. .Net Discovery系列之十一-深入理解平台机制与性能影响 (中)

    上一篇文章中Aicken为大家介绍了.Net平台的垃圾回收机制与其对性能的影响,这一篇中将继续为大家介绍.Net平台的另一批黑马—JIT.   有关JIT的机制分析   ● 机制分析   以C#为例, ...

  3. 让AngularJS的controllers之间共享数据

    如何让controller之间共享数据呢?大致是让不同controller中的变量指向同一个实例. 通过service创建一个存放共享数据的对象. .service("greeting&qu ...

  4. 比Wireshark更轻量、更方便的抓包软件:Charles

    转:http://blog.csdn.net/lixing333/article/details/42776187 之前写过一篇通过Wireshark进行抓包,分析网络连接的文章<通过WireS ...

  5. C++关键字之virtual

    from://http://blog.csdn.net/xuyuanfan/article/details/9935533 在C++中是没有接口的,要真正实现java中的interface功能,需要使 ...

  6. Netty4.0学习笔记系列之二:Handler的执行顺序

    Handler在netty中,无疑占据着非常重要的地位.Handler与Servlet中的filter很像,通过Handler可以完成通讯报文的解码编码.拦截指定的报文.统一对日志错误进行处理.统一对 ...

  7. mysql中的日期转换函数(类似oracle中的to_date)

    最近使用mysql开发一个项目,发现没有像之前在用oracle数据中那样有to_date方法.mysql中实现日期格式化的方法为date_to_str('日期字符串','日期格式'),而且mysql中 ...

  8. Android实现图片裁剪

    MainActivity.java package com.kale.imagetailor; import java.io.File; import java.io.FileNotFoundExce ...

  9. List集合中的数据按照某一个属性进行分组

    有的时候,我们需要在java中对集合中的数据进行分组运算.例如:Bill对象有money(float)和type(String)属性,现有个集合List<Bill>,需要按照Bill的ty ...

  10. 14 道 JavaScript 题?

    http://perfectionkills.com/javascript-quiz/ https://www.zhihu.com/question/34079683 著作权归作者所有.商业转载请联系 ...