1、进入宝马官网,查找经销商查询界面
2、使用火狐浏览(需要安装Firebug和HttpFox)
3、查看json数据以后,json中包含省份,城市,店面类型,经销商信息,并且发现里面的经销商数据中包含地域的编号信息,所以决定制作省份字典、城市字典、类型字典,并且和经销商中数据进行比对输出。
4、得到省份信息主要代码:
1defget_province_dict(self):2province_dict={}3#创建省份信息字典4json_data=urllib2.urlopen(self.index_url).read()5#读取json页面6jsons=json_data.split(';')7#将几组json数据分开8json_province=jsons[0][jsons[0].index('=')+1:-1]9#jsons[0]是省份信息10json_province=json_province+']'11#将得到的字符串整理成正常的json数据格式12provinces=json.loads(json_province)13#读取json数据14forprovinceinprovinces:15province_dict[province.get('id')]=province.get('nz')16#得到ID和省份名称存入相应的字典中17returnprovince_dict
5、得到城市信息的方法遇上面一样
1defget_city_dict(self):2city_dict={}3json_data=urllib2.urlopen(self.index_url).read()#读取json数据4#printjson_data5jsons=json_data.split(';')6#printjsons[1]#城市信息7json_city=jsons[1][jsons[1].index('=')+1:-1]8json_city=json_city+']'9citys=json.loads(json_city)10#printprovinces11forcityincitys:12#printprovince.get('nz')13city_dict[city.get('id')]=city.get('nz')1415forkeyincity_dict:#测试字典16printkey17printcity_dict[key]18returncity_dict6、获得店面类型的信息也类似
defget_type_dict(self):type_dict={}json_data=urllib2.urlopen(self.index_url).read()#读取json数据#printjson_datajsons=json_data.split(';')#printjsons[2]#店面类型信息json_type=jsons[2][jsons[2].index('=')+1:-1]json_type=json_type+']'types=json.loads(json_type)#printprovincesfortypeaintypes:#printprovince.get('nz')type_dict[typea.get('id')]=typea.get('nz')returntype_dict7、由于json中店面的类型是通过ID与类型ID进行匹配的,所以需要将类型的名称与店面id进行匹配制成字典
1defget_dealer_type_dict(self):2dealer_type_dict={}3types=self.get_type_dict()4#调用之前的类型方法,用于后面的匹配5json_data=urllib2.urlopen(self.index_url).read()#读取json数据6#printjson_data7jsons=json_data.split(';')8#printjsons[4]#店面与类型关系信息9json__delaer_type=jsons[4][jsons[4].index('=')+1:-1]10json__delaer_type=json__delaer_type+']'11delaer_types=json.loads(json__delaer_type)12#printprovinces13fordelaer_typeindelaer_types:#有用31-34编号的信息不是所需信息搜易使用if剔除14ifdelaer_type.get('tp')==31:15continue16ifdelaer_type.get('tp')==32:17continue18ifdelaer_type.get('tp')==33:19continue20ifdelaer_type.get('tp')==34:21continue22printdelaer_type.get('tp')23dealer_type_dict[delaer_type.get('br')]=types[delaer_type.get('tp')]24returndealer_type_dict8、处理经销商数据方法
最终结果是
我是爬虫新手,学python也就一个月,还是有高人指点的,代码很冗余,希望对新手有帮助,更希望高手提出意见,我加紧改进学习!!!!!!