网址:aHR0cHM6Ly9jaGFuZ3NoYS55aWNoZS50YW9jaGVjaGUuY29tL2J1eWNhci9wYWdlczlieGNkemFvcXRybm1sLw==
目标:爬取车辆的信息,包括价格,车型,里程,首付等相关信息
按搜索信息找到相应的接口
POST请求,需要的载荷为
import requests
import csv
f = open('涛车车.csv',mode='w',encoding='utf-8',newline='')
csv_writer = csv.DictWriter(f,fieldnames=[
"车型",
'新车售价',
'首付价格',
"款式",
"行驶里程",
"上架时间",
"城市",
])
csv_writer.writeheader()
url = 'https://proconsumer.taocheche.com/c-car-consumer/recommend/searchRecommend'
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
"Cookie":"auto_id=1af2b3336eb2390682193579b05c8aae; uuid=2e8a0342-2c87-4fb7-989c-a9a8d5808bc0; _utrace=2e8a0342-2c87-4fb7-989c-a9a8d5808bc0; city=%7B%22cityId%22%3A302%2C%22cityName%22%3A%22%E5%8E%A6%E9%97%A8%22%2C%22citySpell%22%3A%22xiamen%22%2C%22cityCode%22%3A%22350200%22%2C%22storeId%22%3A314339%7D; storeId=314339; location_citys=%7B%22cityName%22%3A%22%u957F%u6C99%22%2C%22cityId%22%3A1301%2C%22citySpell%22%3A%22changsha%22%7D; history_citys=%5B%7B%22cityId%22%3A3101%2C%22cityAreaId%22%3A500100%2C%22cityName%22%3A%22%u91CD%u5E86%22%2C%22citySpell%22%3A%22chongqing%22%2C%22uri%22%3A%22/chongqing%22%2C%22hasStore%22%3Anull%2C%22searchText%22%3A%22%u91CD%u5E86chongqing%22%2C%22stores%22%3Anull%2C%22poiCity%22%3Anull%7D%5D; cur_city_new=%7B%22cityId%22%3A3101%2C%22cityAreaId%22%3A500100%2C%22cityName%22%3A%22%u91CD%u5E86%22%2C%22citySpell%22%3A%22chongqing%22%2C%22uri%22%3A%22/chongqing%22%2C%22hasStore%22%3Anull%2C%22searchText%22%3A%22%u91CD%u5E86chongqing%22%2C%22stores%22%3Anull%2C%22poiCity%22%3Anull%7D; t_city=3101; is_choose_city=3101"
}
data = {
"BusiType": 6,
"CityId": 3101,
"ShowCount":-1,
"platform": "m",
"userid": "2e8a0342-2c87-4fb7-989c-a9a8d5808bc0"
}
response = requests.post(url=url,json=data,headers=headers)
json_data = response.json()
for index in json_data['data']['carList']:
dit = {
"车型": index['brandName'],
'新车售价':index['NewCarPrice'],
'首付价格':index['downPayment'],
"款式":index['carName'],
"行驶里程":index['drivingMileageText'],
"上架时间":index['buyCarDateText'],
"城市":index['cityName'],
}
print(dit)
csv_writer.writerow(dit)
结果展现: