## Python 懂车帝口碑分爬虫
### 需求
懂车帝全系车型懂车分(口碑)页面中的详细车系评分数据
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/d5946daefb144f82b5dfd6e520948000.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
### 操作环境
- win10
- Google nexus5x(root)
- Python3.9
- Charles
### 需求分析
先来web端试下能否找到需要的数据接口,随便找个车型打开口碑页面F12查看Network
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/9d388cf2fe074ba2982aa37ab261bc7c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
根据页面关键字先搜索一波
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/a38454d5e74141948f57f43b6c981ed0.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
只在页面的标签中找到了数据,没有发现有明显的数据接口,其实用charles抓包的时候发现一个疑似的js好像数据是通过这个js加载出来的,打开看了一下数据相当混乱,暂且先放一边,先从APP分析一波看能否直接拿到数据接口
PS:手机环境、抓包环境的配置在这不在赘述,有兴趣的可参考之前的文章 [APP抓包环境配置](https://cenjy.blog.csdn.net/article/details/123635939)
下载懂车帝APP,并安装至手机
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/43d931bba35b499aa179123e79c453f8.png)
手机开启Postern,pc打开charles
至此抓包工作准备完成,打开懂车帝APP,随便找个车型进入懂车分页面
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/a622e12e31d848e28e8cb0d7a489fd82.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
拿到加载的数据包,和web一样先搜索一波,看能否直接找到需要的数据
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/7b3603baaec748a881df4846bf4170df.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_15,color_FFFFFF,t_70,g_se,x_16)
直接匹配到了多个结果,看接口是同一个接口返回的数据,双击点进去看一下详细数据
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/0b32375fe9884d37b367c9fbf7d44b8d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/43d0f75c691a4de9a5ffeee5574657c6.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_13,color_FFFFFF,t_70,g_se,x_16)
看数据结构和具体的数值和页面中的数据很像,Charles界面太小,将数据拷贝至网页中解析,方便分析,分享一个常用的[json数据在线解析网站](https://www.bejson.com/jsonviewernew/?tdsourcetag=s_pcqq_aiomsg)
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/5f28db88f7b541c2a7349ce2da993320.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
经过仔细分析,APP中展示的数据小数点第二位是四舍五入的,从下图的web页面中可以看出,数据是一样的,成功拿到数据接口!
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/9988cf19b62f4c15b2a22b51c41e900a.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
口碑分数据接口:
```
https://*******/get_detail/?series_id=4182&car_id=0&only_owner=0&year_id=all&iid=2467735824764398&device_id=40011211486215&ac=wifi&channel=dcd-yd-11zh-and-74&aid=36&app_name=automobile&version_code=693&version_name=6.9.3&device_platform=android&os=android&ab_client=a1%2Cc2%2Ce1%2Cf2%2Cg2%2Cf7&ab_group=3167590%2C3577236%2C3333988&ssmix=a&device_type=Nexus+5X&device_brand=google&language=zh&os_api=27&os_version=8.1.0&manifest_version_code=693&resolution=1080*1794&dpi=420&update_version_code=6931&_rticket=1648907286543&cdid=f3163204-7faf-45d7-89c4-e82215c3216c&city_name=%E8%81%8A%E5%9F%8E&gps_city_name=%E8%81%8A%E5%9F%8E&selected_city_name&rom_version=27&longi_lati_type=1&longi_lati_time=1648907102913&content_sort_mode=0&total_memory=1.77&cpu_name=Qualcomm+Technologies%2C+Inc+MSM8992&overall_score=4.873&cpu_score=4.8872&host_abi=
```
对!你没看错,就是这么长,验证一下数据接口,在网页中直接请求一下这个url
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/73fa607f3184429493fec52ba008da60.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
这儿推荐安装一个网页json可视化的插件,这儿偷懒没装,在线解析了一下json数据,和Charles抓到的数据是一样,经过分析得知: series_id是车系id,修改此参数即可
### 获取全部车系id
获取车系id就很简单了,先拿到品牌id然后根据品牌id请求车系信息,注意这是一个post接口
```
def get_series(self, brand_id):
"""
获取品牌所有车系
brand_id:品牌id
"""
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'}
param = {
'offset': 0,
'limit': 1000,
'is_refresh': 1,
'city_name': '北京',
'brand': brand_id
}
response = requests.post(url=url, data=param, headers=headers)
rep_json = json.loads(response.text)
# print(response.text)
if rep_json['status'] == 'success':
return rep_json['data']['series']
else:
raise Exception("get car series has exception!")
```
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/9a6e78ead17c4e78ab18024dd4caf841.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
### 获取车系口碑分
实际的运行过程中,发现一个问题,不同的城市同价位平均分是不一样的,所以需要指定城市获取
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/122f65249595493eb1aa3c1ebbda8d0e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/5f7f688932a3480caccc0af3015e107f.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
```
def get_score(self, series_id, city):
"""
获取车系口碑分
series_id: 车系id
"""
response = self._parse_url(url).json()
# 当前车系评分 综合
series_info = list()
series_info.append(response.get('data').get('series_info').get('score'))
# 当前车系评分 详细
score_info = response.get('data').get('score_info')
if not score_info:
return ['-'] * 16
score = series_info + [i.get('value') for i in score_info]
# 同价位平均分 综合
tab_info_score = list()
if response.get('data').get('tab_info_list'):
tab_info_score.append(response.get('data').get('tab_info_list')[0].get('info')[0].get('score'))
tab_info_score_info = response.get('data').get('tab_info_list')[0].get('info')[0].get('score_info')
tab_info_score_info = tab_info_score + [i.get('value') for i in tab_info_score_info]
else:
tab_info_score_info = ['-'] * 8
data = score + tab_info_score_info
return data
```
### 运行结果
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/7a2be25182f74abbb835afbd72c7ad5b.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
![请添加图片描述](https://ucc.alicdn.com/images/user-upload-01/0667ef099a5f41dc9047c8520c7f0b8e.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ouJ54Gv55qE5bCP5omL,size_20,color_FFFFFF,t_70,g_se,x_16)
### 资源下载
[https://download.csdn.net/download/qq_38154948/85072078](https://download.csdn.net/download/qq_38154948/85072078)
------
本文仅供学习交流使用,如侵立删! |
------