將前面爬蟲所得數(shù)據(jù)進(jìn)行一個(gè)簡單的分析
- 使用charts進(jìn)行展示兑燥,charts是highchats的一個(gè)與python有關(guān)的庫亮瓷,但注意在官網(wǎng)下的可能不能用,需要替換文件降瞳。
簡單用法:charts.plot(series寺庄,show = ’inline‘,options)
series格式為一個(gè)列表力崇,里面是字典,如
series = [{'name': 'John','data': [5],'type': 'column'},{'name': 'John','data': [5],'type': 'column'}]赢织,里面的key為 name,data,type.
show表示在該網(wǎng)頁內(nèi)顯示亮靴,options可以設(shè)置標(biāo)題啥的。 - 采用了生成器來生成series所需的數(shù)據(jù)格式
- 前面進(jìn)行數(shù)據(jù)的篩選于置,合并茧吊。
import pymongo,charts
client = pymongo.MongoClient('localhost',27017)
tongcheng = client['tongcheng']
info_list = tongcheng.info_list
area_list = []
area_index = []
area_count = []
for i in info_list.find():
if i['area'].startswith('北京'):
area_list.append(i['area'][-2:])
area_index = list(set(area_list))
for area in area_index:
area_count.append(area_list.count(area))
#print(area_index)
def gen_data(types):
length = 0
if length <= len(area_index):
for area,times in zip(area_index,area_count):
data ={
'name':area,
'data':[times],
'type':types
}
yield data
length += 1
series = [data for data in gen_data('column')]
charts.plot(series,show = 'inline',options = dict(title=dict(text='北京交易')))