汽车车系爬取.md

概述

仅用于分享学习使用

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

const element = document.querySelector('.tab-content-item');
function page() {
const e = element.querySelector('#boxZ');
console.log('读取中...请稍后...');
if(!e || e.style.display == 'none') {
document.childNodes[1].scrollTop = document.childNodes[1].scrollHeight;
setTimeout(() => page(), 1000);
}
else read();
}

function read() {
let model = [];
let type = [];

// 关联品牌和型号
let modelMap = {}; // 用来存储品牌 ID 和其对应的型号数组

element.querySelectorAll('.uibox-con > dl').forEach((it) => {
let brandId = it.id;
let brandName = it.querySelector('div > a').innerText;

// 保存品牌数据
model.push({
id: brandId,
name: brandName
});

// 初始化该品牌的型号数组
modelMap[brandId] = [];

// 保存每个品牌下的车系数据
it.querySelectorAll('li').forEach((i) => {
if (i.id) {
let typeInfo = {
id: i.id,
model: brandId,
name: i.querySelector('h4 > a').innerText
};
type.push(typeInfo);

// 将车系数据加入对应品牌的数组中
modelMap[brandId].push(typeInfo);
}
});
});

console.log('!!!读取完毕!!!');
console.log('以下为品牌 JSON,点击末尾的复制按钮即可:');
// 按 id 升序排列品牌
console.log(JSON.stringify(model.sort((a, b) => a.id - b.id)));

console.log('以下为型号 JSON,点击末尾的复制按钮即可:');
// 替换 id 中的字母然后将其按升序排列
console.log(JSON.stringify(type.sort((a, b) => a.id.replace(/[a-z]+/g, '') - b.id.replace(/[a-z]+/g, ''))));

console.log('以下为品牌与车系关联的 JSON:');
console.log(JSON.stringify(model.map(m => ({
id: m.id,
name: m.name,
types: modelMap[m.id].sort((a, b) => a.id.replace(/[a-z]+/g, '') - b.id.replace(/[a-z]+/g, ''))
}))));
}

page();

车品牌JSON 转为 CSV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import json
import csv

# 读取 JSON 文件
with open('data.json', 'r', encoding='utf-8') as json_file:
data = json.load(json_file)

# 写入 CSV 文件
with open('data.csv', 'w', newline='', encoding='utf-8') as csv_file:
csv_writer = csv.writer(csv_file)

# 写入 CSV 表头
csv_writer.writerow(['id', 'name'])

# 写入数据行
for item in data:
csv_writer.writerow([item['id'], item['name']])

print("JSON 已成功转换为 CSV 文件。")

车系JSON 转为 CSV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import json
import csv

# 读取 JSON 文件
with open('car_data.json', 'r', encoding='utf-8') as json_file:
data = json.load(json_file)

# 写入 CSV 文件
with open('car_serial.csv', 'w', newline='', encoding='utf-8') as csv_file:
csv_writer = csv.writer(csv_file)

# 写入 CSV 表头
csv_writer.writerow(['id', 'brand_id', 'name'])

# 处理 JSON 数据并写入到 CSV
for brand in data:
brand_id = int(brand['id']) # 提取品牌 ID 的整数部分

for car_type in brand['types']:
# 提取车系 ID 的整数部分,去掉 "s"
car_id = int(car_type['id'].replace('s', ''))

# 获取车系的名称
car_name = car_type['name']

# 写入 CSV 文件 (id, brand_id, name)
csv_writer.writerow([car_id, brand_id, car_name])

print("JSON 已成功转换为 CSV 文件。")


汽车车系爬取.md
https://abrance.github.io/2024/09/18/mdstorage/domain/爬虫/汽车车系爬取/
Author
xiaoy
Posted on
September 18, 2024
Licensed under