python爬虫——汽车之家数据
相信很多买车的朋友,首先会在网上查资料,对比车型价格等,首选就是“汽车之家”,于是,今天我就给大家扒一扒汽车之家的数据:
一、汽车价格:
首先获取的数据是各款汽车名称、价格范围以及最低指导价:
def get_oa_price(self):
try:
oa_price_data_list=[]
for page in range(1,27):
oa_price_api = f"https://price.16888.com/gz/search-0-0-0-0-0-0-0-0-0-1-0-0-0-0-{page}.html"
response = self.sc.get_html(oa_price_api)
if not response:
print('城市页请求失败')
return 0
#燃油车数据块
oa_data_= re.findall(r'<div class="style-box ">\s+<ul class="clearfix">([\s\S]*?)</ul>',response.text)[0]
#燃油车id和名字列表
car_id_name_list = re.findall(r'data-sid="(\d+)" data-name="(.*?)">',oa_data_)
# 价格范围列表
price_range_list = re.findall(fr'<p>(.*?)\s+<span class="', response.text) if len(car_id_name_list)==len(price_range_list):
for index,car_list in enumerate(car_id_name_list):
car_id,car_name = car_list
#价格范围
price_range = price_range_list[index]
#最低价
price_min = int(eval(price_range.split('-')[0])*10000)
oa_price_data_list.append((int(car_id),car_name,price_range,price_min))
# print(price_min)
if not oa_price_data_list or not len(oa_price_data_list):
return 0
print(oa_price_data_list)
print("燃油车价格已经爬取完成")
return 1
except:
self.sc.collect_error()
结果输出如下:
二、汽车销量:
一般买东西,尤其网上买东西,一看价格,二看销量。销量好不好有时候也会决定买不买:
def get_ea_sale(self):
try:
ea_sale_data_list = []
for year in range(2018,2021):
for month in range(1, 13):
if month>9:
date_ = str(year) + str(month)
else:
date_ = str(year) + "0" + str(month)
for i in range(1,3):
ea_sale_api = f"https://xl.16888.com/ev-{date_}-{date_}-{i}.html"
print(ea_sale_api)
response = self.sc.get_html(ea_sale_api)
if not response:
print('城市页请求失败')
return 0
re_no = re.findall(r'<p>暂时没有 <em>电动车</em> <em>\d+.\d+</em> 的销量数据</p>',response.text)
if re_no and len(re_no):
print("没第二页")
break
# 销量数据块
ea_sale_data_ = re.findall(r'<th width="\w+">车型相关</th>([\s\S]*?)<div class="xl-data-pageing lbBox">',
response.text)[0]
# 燃油车id和名字列表
car_id_name_list = re.findall(r'<td class="xl-td-\w+"><a href="/s/(\d+)/" target="_blank">(.*?)</a></td>', ea_sale_data_)
# 销量列表
sale_list = re.findall(r'<td class="xl-td-t3">(\d+)</td>', ea_sale_data_)
if len(car_id_name_list) == len(sale_list):
for index, car_list in enumerate(car_id_name_list):
car_id, car_name = car_list
# 价格范围
sale_num = int(sale_list[index])
ea_sale_data_list.append((int(car_id), car_name, sale_num,date_))
#没有列表数据
if not ea_sale_data_list or not len(ea_sale_data_list):
return 0
print(ea_sale_data_list)
print("电动车销量已经爬取完成")
return 1
except:
self.sc.collect_error()
结果输出如下:
三、汽车评论:
俗话说:买东西看三宝,一看价格,二看销量,三看评论。
def car_comment(self):
try:
ea_com_api = f"https://k.autohome.com.cn/ajax/getSceneSelectCar?minprice=2&maxprice=110&_appid=koubei&fueltype=4"
response = self.sc.get_html(ea_com_api)
if not response:
print('车型列表请求失败')
return 0
ea_com_json=json.loads(response.text)
# print(ea_com_json)
result_list = ea_com_json['result']
for result in result_list:
ea_com_data_list = []
car_id = int(result['SeriesId'])
car_name = result['SeriesName']
print(car_name)
com_api = f"https://k.autohome.com.cn/{car_id}/index_1.html"
com_resp = self.sc.get_html(com_api)
if not com_resp:
print('口碑列表请求失败')
continue
#查看口碑的条数
com_num_list = re.findall(r'<span class="fn-right \w+">共有(\d+)条口碑</span>',com_resp.text)
if not com_num_list or not len(com_num_list):
print("没有口碑")
api_ip = 'http://ip.dobel.cn/switch-ip'
api_ip_resp = self.sc.get_html(api_ip)
time.sleep(1)
com_api = f"https://k.autohome.com.cn/{car_id}/index_1.html"
com_resp = self.sc.get_html(com_api)
if not com_resp:
print('口碑列表请求失败=========')
continue
# 查看口碑的条数
com_num_list = re.findall(r'<span class="fn-right \w+">共有(\d+)条口碑</span>', com_resp.text)
if not com_num_list or not len(com_num_list):
print("没有口碑=========")
continue
com_num = int(com_num_list[0])
if com_num>15:
#翻页
page_num_list = re.findall(r"<span class='page-item-info'>共(\d+)页</span>",com_resp.text)
if not page_num_list or not len(page_num_list):
print("没有口碑")
page_num = 1
else:
page_num = int(page_num_list[0])
else:
page_num = 1
for page in range(1,page_num+1):
com_api2 = f"https://k.autohome.com.cn/{car_id}/index_{page}.html"
print(com_api2)
com_resp2 = self.sc.get_html(com_api2)
if not com_resp2:
print('口碑列表2请求失败')
api_ip = 'http://ip.dobel.cn/switch-ip'
api_ip_resp = self.sc.get_html(api_ip)
time.sleep(1)
com_resp2 = self.sc.get_html(com_api2)
if not com_resp2:
print('口碑列表3请求失败')
continue
#评论id和评论链接
com_id_url_list = re.findall(r'发表了口碑\s+<a href="(.*?)"',com_resp2.text)
if not com_id_url_list or not len(com_id_url_list):
print("没有口碑id")
api_ip = 'http://ip.dobel.cn/switch-ip'
api_ip_resp = self.sc.get_html(api_ip)
time.sleep(1)
com_resp3 = self.sc.get_html(com_api2)
if not com_resp3:
print('口碑列表3请求失败========')
continue
# 评论id和评论链接
com_id_url_list = re.findall(r'发表了口碑\s+<a href="(.*?)"', com_resp3.text)
if not com_id_url_list or not len(com_id_url_list):
print("没有口碑id======")
continue
for com_id_url in com_id_url_list:
com_url = com_id_url
#以时间戳作为评论id
com_id = str(uuid.uuid4())
ea_com_data_list.append((car_id,car_name,com_id,com_url))
# 没有列表数据
if not ea_com_data_list or not len(ea_com_data_list):
return 0
print(f"汽车之家{car_name}评论id已经爬取完成")
return 1
except:
self.sc.collect_error()
以上就是我的分享,如果有什么不足之处请指出,多交流,谢谢!
如果喜欢,请关注我的博客:https://www.cnblogs.com/qiuwuzhidi/
想获取更多数据或定制爬虫的请点击python爬虫专业定制
python爬虫——汽车之家数据的更多相关文章
- python 爬虫 汽车之家车辆参数反爬
水平有限,仅供参考. 如图所示,汽车之家的车辆详情里的数据做了反爬对策,数据被CSS伪类替换. 观察 Sources 发现数据就在当前页面. 发现若干条进行CSS替换的js 继续深入此JS 知道了数据 ...
- java调用Linux执行Python爬虫,并将数据存储到elasticsearch--(环境脚本搭建)
java调用Linux执行Python爬虫,并将数据存储到elasticsearch中 一.以下博客代码使用的开发工具及环境如下: 1.idea: 2.jdk:1.8 3.elasticsearch: ...
- Python爬虫丨大众点评数据爬虫教程(1)
大众点评数据获取 --- 基础版本 大众点评是一款非常受普罗大众喜爱的一个第三方的美食相关的点评网站. 因此,该网站的数据也就非常有价值.优惠,评价数量,好评度等数据也就非常受数据公司的欢迎. 今天就 ...
- PuppeteerSharp+AngleSharp的爬虫实战之汽车之家数据抓取
参考了DotNetSpider示例, 感觉DotNetSpider太重了,它是一个比较完整的爬虫框架. 对比了以下各种无头浏览器,最终采用PuppeteerSharp+AngleSharp写一个爬虫示 ...
- nodejs爬虫——汽车之家所有车型数据
应用介绍 项目Github地址:https://github.com/iNuanfeng/node-spider/ nodejs爬虫,爬取汽车之家(http://www.autohome.com.cn ...
- 使用python爬虫爬取股票数据
前言: 编写一个爬虫脚本,用于爬取东方财富网的上海股票代码,并通过爬取百度股票的单个股票数据,将所有上海股票数据爬取下来并保存到本地文件中 系统环境: 64位win10系统,64位python3.6, ...
- python爬虫爬取天气数据并图形化显示
前言 使用python进行网页数据的爬取现在已经很常见了,而对天气数据的爬取更是入门级的新手操作,很多人学习爬虫都从天气开始,本文便是介绍了从中国天气网爬取天气数据,能够实现输入想要查询的城市,返回该 ...
- python爬虫——爬取网页数据和解析数据
1.网络爬虫的基本概念 网络爬虫(又称网络蜘蛛,机器人),就是模拟客户端发送网络请求,接收请求响应,一种按照一定的规则,自动地抓取互联网信息的程序.只要浏览器能够做的事情,原则上,爬虫都能够做到. 2 ...
- Python爬虫丨大众点评数据爬虫教程(2)
大众点评数据爬虫获取教程 --- [SVG映射版本] 前言: 大众点评是一款非常受大众喜爱的一个第三方的美食相关的点评网站.从网站内可以推荐吃喝玩乐优惠信息,提供美食餐厅.酒店旅游.电影票.家居装修. ...
随机推荐
- 高精地图技术专栏 | 基于空间连续性的异常3D点云修复技术
1.背景 1.1 高精资料采集 高精采集车是集成了测绘激光.高性能惯导.高分辨率相机等传感器为一体的移动测绘系统.高德高精团队经过多年深耕打造的采集车,具有精度高.速度快.数据产生周期短.自动化程度高 ...
- 振兴中华(蓝桥杯13年第四届省赛真题 JAVA-B组)
思路:因为只能横向或纵向跳到相邻的格子里,所以到'华'字有两种方法:①从左边的中横向跳过来 ②从上边的中纵向跳过来 直接递推即可. 标题: 振兴中华 小明参加了学校的趣味运动会,其中的一个项目是:跳格 ...
- 操作系统实验(一)-Shell编程
操作系统实验:Shell编程 emmmmm,实验前老师发了一份实验说明,里面有教怎么配置虚拟机Ubuntu.这里就不做过多叙述,需要说明的是,kali和ubuntu都可以以shell运行这个C语言程序 ...
- 攻防世界 reverse 进阶 -gametime
19.gametime csaw-ctf-2016-quals 这是一个小游戏,挺有意思的 's'-->' ' 'x'-->'x' 'm'-->'m' 观察流程,发现检验函 ...
- 使用命令行编译Qt程序
code[class*="language-"], pre[class*="language-"] { color: rgba(51, 51, 51, 1); ...
- windows平台rust安装
1.安装目录环境变量 RUSTUP_HOME D:\WorkSoftware\Rust\cargo CARGO_HOME D:\WorkSoftware\Rust\rustup 2.安装下载加速环境变 ...
- 201871030106-陈鑫莲 实验二 个人项目-《D{0-1} KP 问题》项目报告
项目 内容 课程班级博客链接 班级博客 这个作业要求链接 作业要求 我的课程学习目标 1.掌握软件项目个人开发流程2.掌握Github发布软件项目的操作方法 这个作业在哪些方面帮助我实现学习目标 1. ...
- 周爱民带你深入剖析JavaScript核心原理
作为前端工程师必备技能,JavaScript 的重要性不言而喻.虽然易上手,但却有着诸多复杂微妙的机制,想要真正掌握绝非易事. 专栏面向JavaScript语言的实际应用者与深度爱好者,以讲述Java ...
- Kubernetes部署metrics-server提示健康检测报错500,简单解决方式
为什么写? 最近有项目要用到HPA(Horizontal Pod Autoscaler)依赖了k8s的 metrics 指标才能做出自动缩扩容的动作,我这边用官方GitHub v0.4.2版本启动不起 ...
- 2021 DevOpsDays 东京站完美收官 | CODING 专家受邀分享最新技术资讯
DevOpsDays 是一个全球知名的系列技术会议品牌,内容涵盖了软件开发.自动化.测试.安全.组织文化以及 IT 运营的社区会议等.DevOpsDays 由 DevOps 之父 Patrick De ...