將前面爬蟲所得數據進行一個簡單的分析
- 使用charts進行展示,charts是highchats的一個與python有關的庫,但注意在官網下的可能不能用,需要替換文件。
簡單用法:charts.plot(series,show = ’inline‘,options)
series格式為一個列表,里面是字典,如
series = [{'name': 'John','data': [5],'type': 'column'},{'name': 'John','data': [5],'type': 'column'}],里面的key為 name,data,type.
show表示在該網頁內顯示,options可以設置標題啥的。 - 采用了生成器來生成series所需的數據格式
- 前面進行數據的篩選,合并。
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='北京交易')))
chart.jpeg