Python爬虫实战笔记_3-3

2016-07-13  本文已影响38人  Sugeei
find()函数的进阶用法
select column1, column2
from table
where key = value
源码
def getdaterange(startfrom, enddate):
    #  input: ('2016-01-01', '2016-01-07')
    #  ouput: ['2016-01-01', '2016-01-02', '2016-01-03',  '2016-01-04', '2016-01-05', '2016-01-06', '2016-01-07']
    stastamp = time.mktime(time.strptime(startfrom,"%Y-%m-%d")) 
    endstamp = time.mktime(time.strptime(enddate,"%Y-%m-%d")) 
    datelist = []
    for i in range(int((endstamp - stastamp)/3600/24)):
        datelist.append(time.strftime("%Y-%m-%d", time.localtime(stastamp + i*3600*24)))
    return datelist
    
def get_statistic_by_daterange(data, daterange):
    #  input: data = {}, daterange = ['2016-07-02', '2016-07-03', '2016-07-04', '2016-07-05', '2016-07-06', '2016-07-07', '2016-07-08']
    #  ouput: {'2016-07-07': 15, '2016-07-04': 7, '2016-07-02': 35, '2016-07-08': 36, '2016-07-06': 13, '2016-07-05': 10, '2016-07-03': 9}
    statistic = {}
    for time in daterange:
        statistic[time] = 0
    for item in data:
        itdate = item['pubtime'][0]
        if itdate in daterange:
            statistic[itdate] += 1
    print(statistic)
    return [item[1] for item in sorted(statistic.items())]#.values()
stadate = time.strftime("%Y-%m-%d", time.localtime(time.time() - 7 * 3600 * 24 ))
enddate = time.strftime("%Y-%m-%d", time.localtime(time.time()))
datelist = getdaterange(stadate, enddate)
print(datelist )
for item in ['笔记本电脑', '手机', '台式电脑整机']:
    datalist.append(get_statistic_by_daterange(tinfo.find({'category': item}), datelist))
#print(data)
运行结果
上一篇下一篇

猜你喜欢

热点阅读