宝马-中国官方网站服务站点信息爬去记录(解析json中数据)

时间:2021-10-03 01:09:56

具体步骤:

1、进入宝马官网,查找经销商查询界面

http://www.bmw.com.cn/cn/zh/general/dealer_locator/content/dealer_locator.html

2、使用火狐浏览(需要安装Firebug和HttpFox)

找到json数据存储位置:https://secure.bmw.com.cn/cn/_common/_js/dealer_locator/dealer_locator.json

3、查看json数据以后,json中包含省份,城市,店面类型,经销商信息,并且发现里面的经销商数据中包含地域的编号信息,所以决定制作省份字典、城市字典、类型字典,并且和经销商中数据进行比对输出。

4、得到省份信息主要代码:

     def get_province_dict(self):
province_dict={}
#创建省份信息字典
json_data = urllib2.urlopen(self.index_url).read()
#读取json页面
jsons = json_data.split(';')
#将几组json数据分开
json_province = jsons[0][jsons[0].index('=')+1:-1]
#jsons[0]是省份信息
json_province = json_province+']'
#将得到的字符串整理成正常的json数据格式
provinces = json.loads(json_province)
#读取json数据
for province in provinces:
province_dict[province.get('id')] = province.get('nz')
#得到ID和省份名称存入相应的字典中
return province_dict

5、得到城市信息的方法遇上面一样

     def get_city_dict(self):
city_dict={}
json_data = urllib2.urlopen(self.index_url).read() #读取json数据
#print json_data
jsons = json_data.split(';')
#print jsons[1]# 城市信息
json_city = jsons[1][jsons[1].index('=')+1:-1]
json_city = json_city+']'
citys = json.loads(json_city)
#print provinces
for city in citys:
#print province.get('nz')
city_dict[city.get('id')] = city.get('nz') for key in city_dict:#测试字典
print key
print city_dict[key]
return city_dict

6、获得店面类型的信息也类似

    def get_type_dict(self):
type_dict={}
json_data = urllib2.urlopen(self.index_url).read() #读取json数据
#print json_data
jsons = json_data.split(';')
#print jsons[2]# 店面类型信息
json_type = jsons[2][jsons[2].index('=')+1:-1]
json_type = json_type+']'
types = json.loads(json_type)
#print provinces
for typea in types:
#print province.get('nz')
type_dict[typea.get('id')] = typea.get('nz')
return type_dict

7、由于json中店面的类型是通过ID与类型ID进行匹配的,所以需要将类型的名称与店面id进行匹配制成字典

     def get_dealer_type_dict(self):
dealer_type_dict={}
types = self.get_type_dict()
#调用之前的类型方法,用于后面的匹配
json_data = urllib2.urlopen(self.index_url).read() #读取json数据
#print json_data
jsons = json_data.split(';')
#print jsons[4]# 店面与类型关系信息
json__delaer_type = jsons[4][jsons[4].index('=')+1:-1]
json__delaer_type = json__delaer_type+']'
delaer_types = json.loads(json__delaer_type)
#print provinces
for delaer_type in delaer_types:#有用31-34编号的信息不是所需信息搜易使用if剔除
if delaer_type.get('tp')==31:
continue
if delaer_type.get('tp')==32:
continue
if delaer_type.get('tp')==33:
continue
if delaer_type.get('tp')==34:
continue
print delaer_type.get('tp')
dealer_type_dict[delaer_type.get('br')] = types[delaer_type.get('tp')]
return dealer_type_dict

8、处理经销商数据方法

     def get_dealer_info(self):
province_dict = self.get_province_dict()
city_dict = self.get_city_dict()
dealer_type_dict = self.get_dealer_type_dict() json_data = urllib2.urlopen(self.index_url).read() jsons = json_data.split(';')
#print jsons[3]#经销商信息
json_dealer = jsons[3][jsons[3].index('=')+1:-1]
#由于此处的json数据过大,致使json.loads()发生异常
#所以选择拼凑成列表的格式进行生成
json_dealer = json_dealer.replace('[','')
json_dealer = json_dealer.replace(']','')
json_dealer = json_dealer.replace('},','}},')
json_dealer = json_dealer.split('},')
#以上为拼凑过程
dealers = list(json_dealer)
#将字符串转变成列表
dealer_info_list = []
for dealer in dealers:
l={}
dealer = json.loads(dealer)
#字符减少可以使用json.loads()进行处理,得到字典
print dealer.get('nz')
l[Constant.PROVINCE] = province_dict[dealer.get('pv')]
#用经销商数据中的省份ID匹配省份字典中的ID,得到中文的省份名称
l[Constant.CITY] = city_dict[dealer.get('ct')]
l[Constant.TYPE] = dealer_type_dict[dealer.get('id')]
l[Constant.NAME] = dealer.get('nz')
l[Constant.ADDRESS] = dealer.get('az')
l[Constant.TEL] = dealer.get('tel')
l[Constant.EMAIL] = dealer.get('em')
l[Constant.WEBSHOP] = dealer.get('ws')
l[Constant.SINA] = dealer.get('wb')
l[Constant.LENDER] = dealer.get('fnz')
l[Constant.LENDERTEL] = dealer.get('ft')
l[Constant.AFTERSALE] = dealer.get('flt')
l[Constant.FAX] = dealer.get('fax')
l[Constant.POSTCODE] = dealer.get('zp')
dealer_info_list.append(l)
self.saver.add(dealer_info_list)#提交保存
self.saver.commit()

还有部分代码是用于将数据存入excel中的,就不贴出来了

最终结果是

宝马-中国官方网站服务站点信息爬去记录(解析json中数据)

我是爬虫新手,学python也就一个月,还是有高人指点的,代码很冗余,希望对新手有帮助,更希望高手提出意见,我加紧改进学习!!!!!!