相信很多买车的朋友,首先会在网上查资料,对比车型价格等,首选就是“汽车之家”,于是,今天我就给大家扒一扒汽车之家的数据:

一、汽车价格:

首先获取的数据是各款汽车名称、价格范围以及最低指导价:

    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>&nbsp;<em>\d+.\d+</em>&nbsp;的销量数据</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爬虫——汽车之家数据的更多相关文章

  1. python 爬虫 汽车之家车辆参数反爬

    水平有限,仅供参考. 如图所示,汽车之家的车辆详情里的数据做了反爬对策,数据被CSS伪类替换. 观察 Sources 发现数据就在当前页面. 发现若干条进行CSS替换的js 继续深入此JS 知道了数据 ...

  2. java调用Linux执行Python爬虫,并将数据存储到elasticsearch--(环境脚本搭建)

    java调用Linux执行Python爬虫,并将数据存储到elasticsearch中 一.以下博客代码使用的开发工具及环境如下: 1.idea: 2.jdk:1.8 3.elasticsearch: ...

  3. Python爬虫丨大众点评数据爬虫教程(1)

    大众点评数据获取 --- 基础版本 大众点评是一款非常受普罗大众喜爱的一个第三方的美食相关的点评网站. 因此,该网站的数据也就非常有价值.优惠,评价数量,好评度等数据也就非常受数据公司的欢迎. 今天就 ...

  4. PuppeteerSharp+AngleSharp的爬虫实战之汽车之家数据抓取

    参考了DotNetSpider示例, 感觉DotNetSpider太重了,它是一个比较完整的爬虫框架. 对比了以下各种无头浏览器,最终采用PuppeteerSharp+AngleSharp写一个爬虫示 ...

  5. nodejs爬虫——汽车之家所有车型数据

    应用介绍 项目Github地址:https://github.com/iNuanfeng/node-spider/ nodejs爬虫,爬取汽车之家(http://www.autohome.com.cn ...

  6. 使用python爬虫爬取股票数据

    前言: 编写一个爬虫脚本,用于爬取东方财富网的上海股票代码,并通过爬取百度股票的单个股票数据,将所有上海股票数据爬取下来并保存到本地文件中 系统环境: 64位win10系统,64位python3.6, ...

  7. python爬虫爬取天气数据并图形化显示

    前言 使用python进行网页数据的爬取现在已经很常见了,而对天气数据的爬取更是入门级的新手操作,很多人学习爬虫都从天气开始,本文便是介绍了从中国天气网爬取天气数据,能够实现输入想要查询的城市,返回该 ...

  8. python爬虫——爬取网页数据和解析数据

    1.网络爬虫的基本概念 网络爬虫(又称网络蜘蛛,机器人),就是模拟客户端发送网络请求,接收请求响应,一种按照一定的规则,自动地抓取互联网信息的程序.只要浏览器能够做的事情,原则上,爬虫都能够做到. 2 ...

  9. Python爬虫丨大众点评数据爬虫教程(2)

    大众点评数据爬虫获取教程 --- [SVG映射版本] 前言: 大众点评是一款非常受大众喜爱的一个第三方的美食相关的点评网站.从网站内可以推荐吃喝玩乐优惠信息,提供美食餐厅.酒店旅游.电影票.家居装修. ...

随机推荐

  1. Python模拟简易版淘宝客服机器人

    对于用Python制作一个简易版的淘宝客服机器人,大概思路是:首先从数据库中用sql语句获取相关数据信息并将其封装成函数,然后定义机器问答的主体函数,对于问题的识别可以利用正则表达式来进行分析,结合现 ...

  2. 循环3n加1问题

    package 第二章; import java.util.Scanner; //int 32位整数 /*  * 猜想:对于任意大于一的自然数n,若n为奇数,则将n变为3n+1,否则变为n的一半 经过 ...

  3. N皇后求解。万万没想到,只用一个一维数组就搞定了。还体现了回溯。

    一.啥是N皇后?先从四皇后入手 给定一个4x4的棋盘,要在棋盘上放置4个皇后.他们的位置有这样的要求,每一列,每一行,每一对角线都能有一个皇后. 你可能会对这个对角线有疑惑,其实就是每一个小正方形的对 ...

  4. [React Hooks长文总结系列一]初出茅庐,状态与副作用

    写在开头 React Hooks在我的上一个项目中得到了充分的使用,对于这个项目来说,我们跳过传统的类组件直接过渡到函数组件,确实是一个不小的挑战.在项目开发过程中也发现项目中的其他小伙伴(包括我自己 ...

  5. python 开发环境安装(最全最完整)

    一.下载安装包 Windows64 位电脑安装 Python,浏览器的地址栏访问: https://www.python.org/ftp/python/3.7.9/python-3.7.9-amd64 ...

  6. (三)Struts2的Action(简单讲解版)

    Actions是Struts2框架的核心,因为它们适用于任何MVC(Model View Controller)框架. 每个URL映射到特定的action,其提供处理来自用户的请求所需的处理逻辑.但a ...

  7. Unity2D项目-平台、解谜、战斗! 1.1战斗底层组件CanFight-CanBeFighted

    各位看官老爷们,这里是RuaiRuai工作室,一个做单机游戏的兴趣作坊. 本文对该2D项目中战斗底层组件的开发及设计思路做一个总结,希望各路同行多多交流,各路大佬多多指点. 实例特征分析 首先对于各个 ...

  8. Dynamics CRM实体系列之图表

    本节开始讲解Dynamics CRM的图表功能.任何产品基本上都会有数据分析的工具,Dynamics CRM当然也不例外,作为一个专门做销售管理的软件数据分析自然也是对于销售管理者的决策有很大的作用的 ...

  9. 解决Docker MySQL无法被宿主机访问的问题

    1 问题描述 Docker启动MySQL容器后,创建一个localhost访问的用户: create user test@localhost identified by 'test'; 但是在宿主机中 ...

  10. Spring 学习笔记(三):Spring Bean

    1 Bean配置 Spring可以看做是一个管理Bean的工厂,开发者需要将Bean配置在XML或者Properties配置文件中.实际开发中常使用XML的格式,其中<bean>中的属性或 ...