├── .gitignore ├── didas-f.py ├── didaspring ├── main.py └── results │ ├── avgpricebar.html │ ├── city.html │ ├── citylines.html │ ├── gender.html │ ├── kepler.json │ └── thanksline.html ├── handle_db.py └── tuhu.py /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | __pycache__ 4 | -------------------------------------------------------------------------------- /didas-f.py: -------------------------------------------------------------------------------- 1 | import json 2 | from handle_db import insert_nearbyroute, insert_cityroute, insert_workroute, update_uesrinfo,create_uesr 3 | import time 4 | 5 | 6 | def trsf_time(intime): 7 | # 时间格式转换的方法,下面转换时间用的 8 | new_time = time.strftime("%Y-%m-%d %H:%M:%S", time.strptime(intime, "%Y%m%d%H%M%S")) 9 | return new_time 10 | 11 | 12 | def response(flow): 13 | nearbyroute_url1 = 'http://211.151.134.222/V3/BookingDriver/getNearbyBookingRideList' 14 | nearbyroute_url2 = 'http://211.151.12.10/V3/BookingDriver/getNearbyBookingRideList' 15 | cityroute_url1 = 'http://211.151.134.222/V3/BookingDriver/getRideListByRoute' 16 | cityroute_url2 = 'http://211.151.12.10/V3/BookingDriver/getRideListByRoute' 17 | workroute_url1 = 'http://211.151.134.222/V3/BookingDriver/getBookingRideList' 18 | workroute_url2 = 'http://211.151.12.10/V3/BookingDriver/getBookingRideList' 19 | user_info_url1 = 'http://211.151.134.222/V3/User/getUserSimpleInfo' 20 | user_info_url2 = 'http://211.151.12.10/V3/User/getUserSimpleInfo' 21 | # 我们需要抓取数据的几个接口URL,APP会不定时改变请求地址,所以每个接口有两个地址 22 | # DiyRoute = 'http://211.151.134.222/V3/BookingDriver/getBookingRideListV2' 23 | if nearbyroute_url1 in flow.request.url or nearbyroute_url2 in flow.request.url: 24 | text = flow.response.content 25 | # 将接口response内容赋值给text,这里需要注意 flow.response.text(已解码) 和 flow.response.content(二进制)的区别 26 | # 一开始用的是text,无奈中文乱码怎麽都搞不定,后来就用context试了一下,成功 27 | text = json.loads(text) 28 | # json.loads 将json格式数据转化成字典 29 | nearbyroutes = text.get('list') 30 | for nearbyroute in nearbyroutes: 31 | user_info = nearbyroute.get('passenger_user_info') 32 | update_uesrinfo(user_info) 33 | print('\033[1;35m 创建用户:\033[0m %s \033[1;35m 手机号:\033[0m %s' % (user_info.get('name'), user_info.get('phone'))) 34 | insert_nearbyroute(nearbyroute) 35 | print('\033[1;36m记录市内订单:\033[0m%s - %s ,时间:%s' % (nearbyroute.get('from_poi').get('short_address'),nearbyroute.get('to_poi').get('short_address'),trsf_time(nearbyroute.get('plan_start_time')))) 36 | 37 | # insert_nearbyroute(nearbyroute) 38 | # award_money = nearbyroute.get('award_money') 39 | # city_id = nearbyroute.get('city_id') 40 | # create_time = trsf_time(nearbyroute.get('create_time')) 41 | # distance = nearbyroute.get('distance') 42 | # driver_received_price = nearbyroute.get('driver_received_price') 43 | # from_poi = nearbyroute.get('from_poi') 44 | # f_business = from_poi.get('business') 45 | # f_city = from_poi.get('city') 46 | # f_city_name = f_city.get('city_name') 47 | # f_provice_name = f_city.get('province_name') 48 | # f_latitude = from_poi.get('latitude') 49 | # f_longitude = from_poi.get('longitude') 50 | # f_long_address = from_poi.get('long_address') 51 | # f_short_address = from_poi.get('short_address') 52 | # f_street = from_poi.get('street') 53 | # id = nearbyroute.get('id') 54 | # passenger_user_info = nearbyroute.get('passenger_user_info') 55 | # cid = passenger_user_info.get('cid') 56 | # gender = passenger_user_info.get('gender') 57 | # name = passenger_user_info.get('name') 58 | # plan_start_time = trsf_time(nearbyroute.get('plan_start_time')) 59 | # price = nearbyroute.get('price') 60 | # thanks_price = nearbyroute.get('thanks_price') 61 | # time_scale_mins = nearbyroute.get('time_scale_mins') 62 | # to_poi = nearbyroute.get('to_poi') 63 | # t_business = to_poi.get('business') 64 | # t_city = to_poi.get('city') 65 | # t_city_name = t_city.get('city_name') 66 | # t_provice_name = t_city.get('province_name') 67 | # t_latitude = to_poi.get('latitude') 68 | # t_longitude = to_poi.get('longitude') 69 | # t_long_address = to_poi.get('long_address') 70 | # t_short_address = to_poi.get('short_address') 71 | # t_street = to_poi.get('street') 72 | # nearbyroute_info = { 73 | # 'award_money': award_money, 74 | # 'city_id': city_id, 75 | # 'create_time': create_time, 76 | # 'distance': distance, 77 | # 'driver_received_price': driver_received_price, 78 | # 'from_poi': { 79 | # 'business': f_business, 80 | # 'city': { 81 | # 'city_name': f_city_name, 82 | # 'province_name': f_provice_name 83 | # }, 84 | # 'latitude': f_latitude, 85 | # 'long_address': f_long_address, 86 | # 'longitude': f_longitude, 87 | # 'short_address': f_short_address, 88 | # 'street': f_street 89 | # }, 90 | # 'id': id, 91 | # 'passenger_user_info': { 92 | # 'cid': cid, 93 | # 'gender': gender, 94 | # 'name': name 95 | # }, 96 | # 'plan_start_time': plan_start_time, 97 | # 'price': price, 98 | # 'thanks_price': thanks_price, 99 | # 'time_scale_mins': time_scale_mins, 100 | # 'to_poi': { 101 | # 'business': t_business, 102 | # 'city': { 103 | # 'city_name': t_city_name, 104 | # 'province_name': t_provice_name 105 | # }, 106 | # 'latitude': t_latitude, 107 | # 'long_address': t_long_address, 108 | # 'longitude': t_longitude, 109 | # 'short_address': t_short_address, 110 | # 'street': t_street 111 | # } 112 | # } 113 | # print(nearbyroute_info) 114 | # create_uesr(passenger_user_info) 115 | # insert_nearbyroute(nearbyroute_info) 116 | # 调用数据库插入方法插入数据 117 | 118 | if workroute_url1 in flow.request.url or workroute_url2 in flow.request.url: 119 | text = flow.response.content 120 | text = json.loads(text) 121 | WorkRoutes = text.get('list') 122 | for WorkRoute in WorkRoutes: 123 | user_info = WorkRoute.get('passenger_user_info') 124 | update_uesrinfo(user_info) 125 | print('\033[1;35m 创建用户:\033[0m %s \033[1;35m 手机号:\033[0m %s' % (user_info.get('name'), user_info.get('phone'))) 126 | insert_workroute(WorkRoute) 127 | print('\033[1;36m记录路线订单:\033[0m%s - %s ,时间:%s' % ( 128 | WorkRoute.get('from_poi').get('short_address'), WorkRoute.get('to_poi').get('short_address'), 129 | trsf_time(WorkRoute.get('plan_start_time')))) 130 | # create_time = trsf_time(WorkRoute.get('create_time')) 131 | # distance = WorkRoute.get('distance') 132 | # driver_received_price = WorkRoute.get('driver_received_price') 133 | # from_poi = WorkRoute.get('from_poi') 134 | # f_business = from_poi.get('business') 135 | # f_city = from_poi.get('city') 136 | # f_city_name = f_city.get('city_name') 137 | # f_provice_name = f_city.get('province_name') 138 | # f_latitude = from_poi.get('latitude') 139 | # f_longitude = from_poi.get('longitude') 140 | # f_long_address = from_poi.get('long_address') 141 | # f_short_address = from_poi.get('short_address') 142 | # f_street = from_poi.get('street') 143 | # id = WorkRoute.get('id') 144 | # passenger_user_info = WorkRoute.get('passenger_user_info') 145 | # cid = passenger_user_info.get('cid') 146 | # gender = passenger_user_info.get('gender') 147 | # name = passenger_user_info.get('name') 148 | # phone = passenger_user_info.get('phone') 149 | # person_num = WorkRoute.get('person_num') 150 | # plan_start_time = trsf_time(WorkRoute.get('plan_start_time')) 151 | # price = WorkRoute.get('price') 152 | # priceText = WorkRoute.get('priceText') 153 | # thanks_price = WorkRoute.get('thanks_price') 154 | # to_poi = WorkRoute.get('to_poi') 155 | # t_business = to_poi.get('business') 156 | # t_city = to_poi.get('city') 157 | # t_city_name = t_city.get('city_name') 158 | # t_provice_name = t_city.get('province_name') 159 | # t_latitude = to_poi.get('latitude') 160 | # t_longitude = to_poi.get('longitude') 161 | # t_long_address = to_poi.get('long_address') 162 | # t_short_address = to_poi.get('short_address') 163 | # t_street = to_poi.get('street') 164 | # WorkRouteInfo = { 165 | # "create_time": create_time, 166 | # "distance": distance, 167 | # "driver_received_price": driver_received_price, 168 | # "from_poi": { 169 | # "business": f_business, 170 | # "city": { 171 | # "city_name": f_city_name, 172 | # "province_name": f_provice_name 173 | # }, 174 | # "latitude": f_latitude, 175 | # "long_address": f_long_address, 176 | # "longitude": f_longitude, 177 | # "short_address": f_short_address, 178 | # "street": f_street 179 | # }, 180 | # "id": id, 181 | # "passenger_user_info": { 182 | # "cid": cid, 183 | # "gender": gender, 184 | # "name": name, 185 | # "phone_num": phone, 186 | # }, 187 | # "person_num": person_num, 188 | # "plan_start_time": plan_start_time, 189 | # "price": price, 190 | # "priceText": priceText, 191 | # "thanks_price": thanks_price, 192 | # "to_poi": { 193 | # "business": t_business, 194 | # "city": { 195 | # "city_name": t_city_name, 196 | # "province_name": t_provice_name 197 | # }, 198 | # "latitude": t_latitude, 199 | # "long_address": t_long_address, 200 | # "longitude": t_longitude, 201 | # "short_address": t_short_address, 202 | # "street": t_street 203 | # }, 204 | # } 205 | # # user_phone = { 206 | # # "cid":cid, 207 | # # "name":name, 208 | # # "phone":phone 209 | # # } 210 | # print(WorkRouteInfo) 211 | # update_uesrinfo(passenger_user_info) 212 | # insert_workroute(WorkRouteInfo) 213 | # # update_uesrinfo(user_phone) 214 | 215 | if cityroute_url1 in flow.request.url or cityroute_url2 in flow.request.url: 216 | text = flow.response.content 217 | text = json.loads(text) 218 | CityRouts = text.get('list') 219 | for CityRout in CityRouts: 220 | user_info = CityRout.get('passenger_user_info') 221 | update_uesrinfo(user_info) 222 | print('\033[1;35m 创建用户:\033[0m %s \033[1;35m 手机号:\033[0m %s' % (user_info.get('name'), user_info.get('phone'))) 223 | insert_cityroute(CityRout) 224 | print('\033[1;36m记录跨城订单:\033[0m%s - %s ,时间:%s' % ( 225 | CityRout.get('from_poi').get('short_address'), CityRout.get('to_poi').get('city').get('city_name'), 226 | trsf_time(CityRout.get('plan_start_time')))) 227 | # id = CityRout.get('id') 228 | # from_poi = CityRout.get('from_poi') 229 | # f_longitude = from_poi.get('longitude') 230 | # f_latitude = from_poi.get('latitude') 231 | # f_short_address = from_poi.get('short_address') 232 | # f_long_address = from_poi.get('long_address') 233 | # f_provice_name = from_poi.get('city').get('province_name') 234 | # f_city_name = from_poi.get('city').get('city_name') 235 | # f_business = from_poi.get('business') 236 | # f_street = from_poi.get('street') 237 | # to_poi = CityRout.get('to_poi') 238 | # t_longitude = to_poi.get('longitude') 239 | # t_latitude = to_poi.get('latitude') 240 | # t_short_address = to_poi.get('short_address') 241 | # t_long_address = to_poi.get('long_address') 242 | # t_provice_name = to_poi.get('city').get('province_name') 243 | # t_city_name = to_poi.get('city').get('city_name') 244 | # t_business = to_poi.get('business') 245 | # t_street = to_poi.get('street') 246 | # price = CityRout.get('price') 247 | # driver_received_price = CityRout.get('driver_received_price') 248 | # thanks_price = CityRout.get('thanks_price') 249 | # plan_start_time = trsf_time(CityRout.get('plan_start_time')) 250 | # create_time = trsf_time(CityRout.get('create_time')) 251 | # passenger_user_info = CityRout.get('passenger_user_info') 252 | # cid = passenger_user_info.get('cid') 253 | # name = passenger_user_info.get('name') 254 | # gender = passenger_user_info.get('gender') 255 | # CityRoutInfo = { 256 | # "id": id, 257 | # "from_poi": { 258 | # "longitude": f_longitude, 259 | # "latitude": f_latitude, 260 | # "short_address": f_short_address, 261 | # "long_address": f_long_address, 262 | # "city": { 263 | # "province_name": f_provice_name, 264 | # "city_name": f_city_name 265 | # }, 266 | # "business": f_business, 267 | # "street": f_street 268 | # }, 269 | # "to_poi": { 270 | # "longitude": t_longitude, 271 | # "latitude": t_latitude, 272 | # "short_address": t_short_address, 273 | # "long_address": t_long_address, 274 | # "city": { 275 | # "province_name": t_provice_name, 276 | # "city_name": t_city_name 277 | # }, 278 | # "business": t_business, 279 | # "street": t_street 280 | # }, 281 | # "price": price, 282 | # "driver_received_price": driver_received_price, 283 | # "thanks_price": thanks_price, 284 | # "plan_start_time": plan_start_time, 285 | # "create_time": create_time, 286 | # "passenger_user_info": { 287 | # "cid": cid, 288 | # "name": name, 289 | # "gender": gender 290 | # }, 291 | # 292 | # } 293 | # print(CityRoutInfo) 294 | # create_uesr(passenger_user_info) 295 | # insert_cityroute(CityRoutInfo) 296 | 297 | if user_info_url1 in flow.request.url or user_info_url2 in flow.request.url: 298 | text = json.loads(flow.response.content) 299 | user_info = text.get('userinfo') 300 | # user_info = json.dumps(user_info) 301 | print('\033[1;35m 创建用户:\033[0m%s' % (user_info.get('name'))) 302 | create_uesr(user_info) 303 | 304 | 305 | -------------------------------------------------------------------------------- /didaspring/main.py: -------------------------------------------------------------------------------- 1 | from pyecharts import Pie,Bar,Line,GeoLines, Style 2 | from pymongo import MongoClient 3 | import json 4 | 5 | global style,geo_style 6 | # 定义图表初始化配置 7 | style = Style( 8 | title_color="#fff", 9 | title_pos="center", 10 | width=1260, 11 | height=640, 12 | background_color="#08192D" 13 | ) 14 | # geo风格配置 15 | geo_style = style.add( 16 | legend_orient="vertical", 17 | legend_pos="left", 18 | legend_top = "center", 19 | legend_text_color="#fff", 20 | is_label_show=True, 21 | line_curve=0.2, 22 | line_opacity=0.6, 23 | geo_effect_symbol="plane", 24 | geo_effect_symbolsize=8, 25 | label_pos="right", 26 | label_formatter="{b}", 27 | label_text_color="#eee", 28 | symbol_size = 0.2, 29 | label_text_size=8, 30 | label_color=['#24936E','#6A4C9C','#0089A7','#BEC23F','#D0104C'] 31 | 32 | ) 33 | # 饼图风格配置 34 | pie_style = style.add( 35 | legend_orient="vertical", 36 | legend_pos="left", 37 | legend_text_color="#fff", 38 | is_label_show=True, 39 | legend_top = "center", 40 | label_text_size=20, 41 | ) 42 | # 柱状图风格配置 43 | bar_style = style.add( 44 | legend_orient="vertical", 45 | legend_pos="left", 46 | legend_text_color="#fff", 47 | is_label_show=True, 48 | label_text_size=20, 49 | legend_top = "center", 50 | is_stack=True, 51 | xaxis_label_textsize=24, 52 | xaxis_label_textcolor="#fff", 53 | yaxis_label_textcolor = "#fff" 54 | ) 55 | # 折线图风格配置 56 | line_style = style.add( 57 | legend_orient="vertical", 58 | legend_pos="left", 59 | legend_text_color="#fff", 60 | is_label_show=True, 61 | legend_top = "center", 62 | label_text_size=24, 63 | is_stack=True, 64 | xaxis_label_textcolor="#fff", 65 | xaxis_label_textsize= 24, 66 | yaxis_label_textcolor = "#fff", 67 | label_color=['#24936E','#D0104C'] 68 | ) 69 | 70 | class SpringData: 71 | SpringData = [] 72 | 73 | def __init__(self): 74 | self.client = MongoClient('mongodb://localhost:27017') 75 | self.spring = self.client.spring 76 | 77 | # 性别分析 78 | def getGender(self): 79 | collection = self.spring['cityroute'] 80 | totalUser = collection.aggregate([{ 81 | '$group':{ 82 | '_id':'$passenger_user_info.gender', 83 | 'Gnum':{ 84 | '$sum': 1 85 | } 86 | } 87 | }]) 88 | attr = ["男乘客","女乘客","性别不详"] 89 | listnum = list(totalUser) 90 | male = listnum[0]['Gnum'] 91 | female = listnum[2]['Gnum'] 92 | nomale = listnum[1]['Gnum'] 93 | value = [male,female,nomale] 94 | genderpie = Pie("性别比例",**style.init_style) 95 | genderpie.add("性别",attr,value, **pie_style) 96 | genderpie.render("results/gender.html") 97 | print(male,female,nomale) 98 | 99 | # 订单数分析 100 | def getCityAll(self): 101 | collection = self.spring['cityroute'] 102 | result_hangzhou = collection.aggregate([ 103 | {'$match':{'from_poi.city.city_name':'杭州'}}, 104 | {'$group':{'_id':'$passenger_user_info.gender','count':{'$sum':1}}} 105 | ]) 106 | result_beijing = collection.aggregate([ 107 | {'$match': {'from_poi.city.city_name': '北京'}}, 108 | {'$group': {'_id': '$passenger_user_info.gender', 'count': {'$sum': 1}}} 109 | ]) 110 | result_shanghai = collection.aggregate([ 111 | {'$match': {'from_poi.city.city_name': '上海'}}, 112 | {'$group': {'_id': '$passenger_user_info.gender', 'count': {'$sum': 1}}} 113 | ]) 114 | result_guangzhou = collection.aggregate([ 115 | {'$match': {'from_poi.city.city_name': '广州'}}, 116 | {'$group': {'_id': '$passenger_user_info.gender', 'count': {'$sum': 1}}} 117 | ]) 118 | result_shenzhen = collection.aggregate([ 119 | {'$match': {'from_poi.city.city_name': '深圳'}}, 120 | {'$group': {'_id': '$passenger_user_info.gender', 'count': {'$sum': 1}}} 121 | ]) 122 | result_hangzhou_ = list(result_hangzhou) 123 | result_hangzhou_male = result_hangzhou_[0]["count"] 124 | result_hangzhou_female = result_hangzhou_[2]["count"] 125 | result_hangzhou_nomale = result_hangzhou_[1]["count"] 126 | result_beijing_ = list(result_beijing) 127 | result_beijing_male = result_beijing_[0]["count"] 128 | result_beijing_female = result_beijing_[2]["count"] 129 | result_beijing_nomale = result_beijing_[1]["count"] 130 | result_shanghai_ = list(result_shanghai) 131 | result_shanghai_male = result_shanghai_[0]["count"] 132 | result_shanghai_female = result_shanghai_[2]["count"] 133 | result_shanghai_nomale = result_shanghai_[1]["count"] 134 | result_guangzhou_ = list(result_guangzhou) 135 | result_guangzhou_male = result_guangzhou_[0]["count"] 136 | result_guangzhou_female = result_guangzhou_[2]["count"] 137 | result_guangzhou_nomale = result_guangzhou_[1]["count"] 138 | result_shenzhen_ = list(result_shenzhen) 139 | result_shenzhen_male = result_shenzhen_[0]["count"] 140 | result_shenzhen_female = result_shenzhen_[2]["count"] 141 | result_shenzhen_nomale = result_shenzhen_[1]["count"] 142 | citybar = Bar("各城市订单情况",page_title = "各城市订单情况",**style.init_style) 143 | attr = ["北京","上海","广州","深圳","杭州"] 144 | male_value = [result_beijing_male,result_shanghai_male,result_guangzhou_male,result_shenzhen_male,result_hangzhou_male] 145 | female_value = [result_beijing_female,result_shanghai_female,result_guangzhou_female,result_shenzhen_female,result_hangzhou_female] 146 | nomale_value = [result_beijing_nomale,result_shanghai_nomale,result_guangzhou_nomale,result_shenzhen_nomale,result_hangzhou_nomale] 147 | citybar.add("男乘客",attr,male_value,**bar_style) 148 | citybar.add("女乘客", attr, female_value,**bar_style) 149 | citybar.add("性别不详", attr, nomale_value,**bar_style) 150 | citybar.render("results/city.html") 151 | 152 | # 路线图分析 153 | def getLines(self): 154 | collection = self.spring['cityroute'] 155 | line_hangzhou = collection.aggregate([ 156 | {'$match': {'from_poi.city.city_name': '杭州'}}, 157 | {'$group': {'_id': '$to_poi.city.city_name', 'count': {'$sum': 1}}} 158 | ]) 159 | line_beijing = collection.aggregate([ 160 | {'$match': {'from_poi.city.city_name': '北京'}}, 161 | {'$group': {'_id': '$to_poi.city.city_name', 'count': {'$sum': 1}}} 162 | ]) 163 | line_shanghai = collection.aggregate([ 164 | {'$match': {'from_poi.city.city_name': '上海'}}, 165 | {'$group': {'_id': '$to_poi.city.city_name', 'count': {'$sum': 1}}} 166 | ]) 167 | line_guangzhou = collection.aggregate([ 168 | {'$match': {'from_poi.city.city_name': '广州'}}, 169 | {'$group': {'_id': '$to_poi.city.city_name', 'count': {'$sum': 1}}} 170 | ]) 171 | line_shenzhen = collection.aggregate([ 172 | {'$match': {'from_poi.city.city_name': '深圳'}}, 173 | {'$group': {'_id': '$to_poi.city.city_name', 'count': {'$sum': 1}}} 174 | ]) 175 | line_hangzhou_ = [] 176 | for line in line_hangzhou: 177 | line_hangzhou_.append(["杭州",line['_id'],line['count']]) 178 | line_beijing_ = [] 179 | for line in line_beijing: 180 | line_beijing_.append(["北京", line['_id'], line['count']]) 181 | line_shanghai_ = [] 182 | for line in line_shanghai: 183 | line_shanghai_.append(["上海", line['_id'], line['count']]) 184 | line_guangzhou_ = [] 185 | for line in line_guangzhou: 186 | line_guangzhou_.append(["广州", line['_id'], line['count']]) 187 | line_shenzhen_ = [] 188 | for line in line_shenzhen: 189 | line_shenzhen_.append(["深圳", line['_id'], line['count']]) 190 | 191 | citylines = GeoLines("春节迁移路线图", **style.init_style) 192 | citylines.add("从北京出发", 193 | line_beijing_, 194 | **geo_style) 195 | citylines.add("从上海出发", 196 | line_shanghai_, 197 | **geo_style) 198 | citylines.add("从广州出发", 199 | line_guangzhou_, 200 | **geo_style) 201 | citylines.add("从深圳出发", 202 | line_shenzhen_, 203 | **geo_style) 204 | citylines.add("从杭州出发", 205 | line_hangzhou_, 206 | **geo_style) 207 | citylines.render("results/citylines.html") 208 | 209 | # 客单价分析 210 | def getAvgPrice(self): 211 | collection = self.spring['cityroute'] 212 | avg_price = collection.aggregate([ 213 | {'$group':{'_id':'$from_poi.city.city_name','avg_price':{'$avg':'$price'}}} 214 | ]) 215 | city_avg_price = list(avg_price) 216 | city_avg_price = sorted(city_avg_price, key=lambda city:city['avg_price']) 217 | pricebar = Bar("各城市顺风车平均单价",page_title="各城市顺风车平均单价",**style.init_style) 218 | attr = [city_avg_price[0]['_id'],city_avg_price[1]['_id'],city_avg_price[2]['_id'],city_avg_price[3]['_id'],city_avg_price[8]['_id']] 219 | value = ['%.2f'%(city_avg_price[0]['avg_price']),'%.2f'%(city_avg_price[1]['avg_price']),'%.2f'%(city_avg_price[2]['avg_price']),'%.2f'%(city_avg_price[3]['avg_price']),'%.2f'%(city_avg_price[8]['avg_price'])] 220 | pricebar.add("城市",attr,value,**bar_style) 221 | pricebar.render("results/avgpricebar.html") 222 | print(city_avg_price) 223 | 224 | # 加价分析 225 | def getThanks(self): 226 | collection = self.spring['cityroute'] 227 | count_thanks = collection.aggregate([ 228 | {'$match':{'thanks_price':{'$gt':0}}}, 229 | {'$group':{'_id':'$from_poi.city.city_name','avg_thanks_price':{'$avg':'$thanks_price'},'count_thanks':{'$sum':1}}} 230 | ]) 231 | thanks_result = list(count_thanks) 232 | thanks_result = sorted(thanks_result,key=lambda city: city['avg_thanks_price']) 233 | print(thanks_result) 234 | thanksline = Line("哪里的乘客最壕气",page_title="哪里的乘客最壕气",**style.init_style) 235 | count_value = ['%.2f'%(thanks_result[3]['count_thanks']/11.74),'%.2f'%(thanks_result[4]['count_thanks']/46.34),'%.2f'%(thanks_result[6]['count_thanks']/17.32),'%.2f'%(thanks_result[7]['count_thanks']/27.15),'%.2f'%(thanks_result[8]['count_thanks']/20.83)] 236 | avg_value = ['%.2f'%(thanks_result[3]['avg_thanks_price']),'%.2f'%(thanks_result[4]['avg_thanks_price']),'%.2f'%(thanks_result[6]['avg_thanks_price']),'%.2f'%(thanks_result[7]['avg_thanks_price']),'%.2f'%(thanks_result[8]['avg_thanks_price'])] 237 | attr = [thanks_result[3]['_id'], thanks_result[4]['_id'], thanks_result[6]['_id'], thanks_result[7]['_id'], 238 | thanks_result[8]['_id']] 239 | thanksline.add("比例",attr,count_value,**line_style) 240 | thanksline.add("平均加价",attr,avg_value,**line_style) 241 | thanksline.render("results/thanksline.html") 242 | 243 | 244 | # 导出Kepler地图格式的json文件 245 | def getKepler(self): 246 | collection = self.spring['cityroute'] 247 | results = list(collection.find({},{"_id":0,"from_poi.longitude":1,"from_poi.latitude":1,"to_poi.longitude":1,"to_poi.latitude":1,})) 248 | kepler_data = [] 249 | for result in results: 250 | kepler_data.append({ 251 | "from_lng":result['from_poi']['longitude'], 252 | "from_lat":result['from_poi']['latitude'], 253 | "to_lng":result['to_poi']['longitude'], 254 | "to_lat":result['to_poi']['latitude'] 255 | }) 256 | kepler_json = json.dumps(kepler_data) 257 | file = open('results/kepler.json','w') 258 | file.write(kepler_json) 259 | file.close() 260 | pass 261 | 262 | 263 | test = SpringData() 264 | test.getKepler() -------------------------------------------------------------------------------- /handle_db.py: -------------------------------------------------------------------------------- 1 | import pymongo 2 | 3 | def insert_nearbyroute(info): 4 | client = pymongo.MongoClient('127.0.0.1', 27017) 5 | # 连接数据库 6 | db = client.spring 7 | # 选择dida这个数据库 8 | nearbyroute_collection = db.nearby 9 | # 选择集合,不存在的话自动创建(对应mysql的表概念) 10 | nearbyroute_collection.update({'id':info['id']}, info, True) 11 | # 插入数据,我这里用的是更新语句,意思是如果id已经存在的话,就不执行该条数据的插入动作,可以有效去重 12 | client.close() 13 | 14 | def insert_cityroute(info): 15 | client = pymongo.MongoClient('127.0.0.1', 27017) 16 | db = client.spring 17 | cityroute_collection = db.cityroute 18 | cityroute_collection.update({'id': info['id']}, info, True) 19 | client.close() 20 | 21 | def insert_workroute(info): 22 | client = pymongo.MongoClient('127.0.0.1', 27017) 23 | db = client.spring 24 | workroute_collection = db.workroute 25 | workroute_collection.update({'id': info['id']}, info, True) 26 | client.close() 27 | 28 | def update_uesrinfo(info): 29 | client = pymongo.MongoClient('127.0.0.1', 27017) 30 | db = client.spring 31 | userinfo_collection = db.userinfo 32 | userinfo_collection.update({'cid': info['cid']}, {"$set": {"cid":info['cid'], "gender": info['gender'], "name": info['name']}}, True) 33 | client.close() 34 | 35 | def create_uesr(info): 36 | client = pymongo.MongoClient('127.0.0.1', 27017) 37 | db = client.spring 38 | userinfo_collection = db.userinfo 39 | userinfo_collection.update({'cid': info['cid']}, info, True) 40 | client.close() 41 | -------------------------------------------------------------------------------- /tuhu.py: -------------------------------------------------------------------------------- 1 | import ssl 2 | import requests 3 | import json 4 | import pymongo 5 | import urllib.parse 6 | import urllib.request 7 | ssl._create_default_https_context = ssl._create_unverified_context 8 | 9 | brandUrl = 'https://api.tuhu.cn/Vehicle/SelectVehicleAllBrands' 10 | vehicleUrl = 'https://api.tuhu.cn/Vehicle/SelectVehicleInfoByBrand' 11 | carUrl = 'https://api.tuhu.cn/Vehicle/SelectVehicle' 12 | 13 | def getDbInsert(info): 14 | 15 | 16 | brandResponse = requests.get(brandUrl) 17 | if brandResponse: 18 | brands = json.loads(brandResponse.text) 19 | brands = brands.get('Brand') 20 | for brand in brands: 21 | brandName = brand.get('Brand') 22 | brandLogoUrl = brand.get('ImageUrl') 23 | # 获取品牌名称 24 | vehicleResponse = requests.get(vehicleUrl, data={'brand': brandName}) 25 | if vehicleResponse: 26 | # 将品牌名称作为参数,获取车系 27 | vehicles = json.loads(vehicleResponse.text).get('OneBrand') 28 | for vehicle in vehicles: 29 | brandType = vehicle.get('BrandType') 30 | carSeries = vehicle.get('CarName') 31 | seriesImage = vehicle.get('Image') 32 | vehicleName = vehicle.get('Vehicle') 33 | vehicleId = vehicle.get('ProductID') 34 | seriesImageSmall = vehicle.get('Src') 35 | tires = vehicle.get('Tires') 36 | # 获取车系ID 37 | plResponse = requests.get(carUrl, data={'vehicleId': vehicleId}) 38 | if plResponse: 39 | pls = json.loads(plResponse.text).get('PaiLiang') 40 | if pls: 41 | for pl in pls: 42 | nianResponse = requests.get(carUrl, data={'vehicleId': vehicleId,'pailiang': pl}) 43 | if nianResponse: 44 | nians = json.loads(nianResponse.text).get('Nian') 45 | for nian in nians: 46 | carResponse = requests.get(carUrl, data={'vehicleId': vehicleId, 'pailiang': pl, 'nian': nian}) 47 | if carResponse: 48 | cars = json.loads(carResponse.text).get('SalesName') 49 | for car in cars: 50 | carInfo = { 51 | "Tid": car['Tid'], 52 | "SalesName": car['SalesName'], 53 | "AvgPrice": car['AvgPrice'], 54 | "BrandName": brandName, 55 | "BrandLogo": brandLogoUrl, 56 | "BrandType": brandType, 57 | "CarSeries": carSeries, 58 | "SeriesImage": seriesImage, 59 | "SeriesID": vehicleId, 60 | "SeriesImage-small": seriesImageSmall, 61 | "Tires": tires, 62 | "Year": nian, 63 | "Displacement": pl, 64 | } 65 | # client = pymongo.MongoClient('127.0.0.1', 27017) 66 | # # 连接数据库 67 | # db = client.dida 68 | # # 选择dida这个数据库 69 | # cars_collection = db.cars 70 | # # 选择集合,不存在的话自动创建(对应mysql的表概念) 71 | # cars_collection.update({'Tid': carInfo['Tid']}, carInfo, True) 72 | # # 插入数据,我这里用的是更新语句,意思是如果id已经存在的话,就不执行该条数据的插入动作,可以有效去重 73 | # # client.close() 74 | print(carInfo) 75 | 76 | 77 | --------------------------------------------------------------------------------