【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='127.0.0.1',user='root',passwd='12345678',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分

posted @ 2018-05-07 13:41  逆火狂飙  阅读(217)  评论(1编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东