{"id":37153903,"url":"https://github.com/go-crawler/car-prices","last_synced_at":"2026-01-14T18:04:13.942Z","repository":{"id":57483584,"uuid":"127591560","full_name":"go-crawler/car-prices","owner":"go-crawler","description":"Golang爬虫 爬取汽车之家 二手车产品库","archived":false,"fork":false,"pushed_at":"2018-04-01T03:12:32.000Z","size":10,"stargazers_count":75,"open_issues_count":1,"forks_count":37,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-06-20T15:57:39.919Z","etag":null,"topics":["crawler","go","golang","spider"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/go-crawler.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-01T03:00:01.000Z","updated_at":"2024-06-10T00:33:03.000Z","dependencies_parsed_at":"2022-08-28T17:40:45.884Z","dependency_job_id":null,"html_url":"https://github.com/go-crawler/car-prices","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/go-crawler/car-prices","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-crawler%2Fcar-prices","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-crawler%2Fcar-prices/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-crawler%2Fcar-prices/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-crawler%2Fcar-prices/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-crawler","download_url":"https://codeload.github.com/go-crawler/car-prices/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-crawler%2Fcar-prices/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28429441,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T16:38:47.836Z","status":"ssl_error","status_checked_at":"2026-01-14T16:34:59.695Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crawler","go","golang","spider"],"created_at":"2026-01-14T18:04:13.309Z","updated_at":"2026-01-14T18:04:13.938Z","avatar_url":"https://github.com/go-crawler.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 爬取汽车之家 二手车产品库\r\n\r\n## 目标\r\n\r\n最近经常有人在耳边提起汽车之家，也好奇二手车在国内的价格是怎么样的，因此本次的目标站点是 [汽车之家](https://car.autohome.com.cn/2sc/440399/index.html) 的二手车产品库\r\n\r\n![image](https://i.loli.net/2018/03/30/5abe47f82a01f.png)\r\n\r\n分析目标源：\r\n- 一页共24条\r\n- 含分页，但这个老产品库，在100页后会存在问题，因此我们爬取99页\r\n- 可以获取全部城市\r\n- 共可爬取 19w+ 数据\r\n\r\n## 开始\r\n\r\n爬取步骤\r\n- 获取全部的城市\r\n- 拼装全部城市URL入队列\r\n- 解析二手车页面结构\r\n- 下一页URL入队列\r\n- 循环拉取所有分页的二手车数据\r\n- 循环拉取队列中城市的二手车数据\r\n- 等待，确定队列中无新的 URL\r\n- 爬取的二手车数据入库\r\n\r\n### 获取城市\r\n\r\n![image](https://i.loli.net/2018/03/31/5abeff11ef583.png)\r\n\r\n通过页面查看，可发现在城市筛选区可得到全部的二手车城市列表，但是你仔细查阅代码。会发现它是JS加载进来的，城市也统一放在了一个变量中\r\n\r\n![image](https://i.loli.net/2018/03/31/5abf056389cf0.png)\r\n\r\n有两种提取方法\r\n- 分析JS变量，提取出来\r\n- 直接将 `areaJson` 复制出来作为变量解析\r\n\r\n在这里我们直接将其复制粘贴出来即可，因为这是比较少变动的值\r\n\r\n### 获取分页\r\n\r\n![image](https://i.loli.net/2018/03/31/5abf08ec812e2.png)\r\n\r\n通过分析页面可以得知分页链接是有一定规律的，例如：`/2sc/hangzhou/a0_0msdgscncgpi1ltocsp2exb4/`，可以发现 `sp%d`，`sp` 后面为页码\r\n\r\n按照常理，可以通过预测所有分页链接，推入队列后 `go routine` 一波 即可快速拉取\r\n\r\n但是在这老产品库存在一个问题，在超过 100 页后，下一页永远是 101 页\r\n\r\n![image](https://i.loli.net/2018/03/31/5abf0e1e623ec.png)\r\n\r\n因此我们采取比较传统的做法，通过拉取下一页的链接去访问，以便适应可能的分页链接改变； 100 页以后的分页展示也很奇怪，先忽视\r\n\r\n### 获取二手车数据\r\n\r\n页面结构较为固定，常规的清洗 HTML 即可\r\n\r\n```\r\nfunc GetCars(doc *goquery.Document) (cars []QcCar) {\r\n\tcityName := GetCityName(doc)\r\n\tdoc.Find(\".piclist ul li:not(.line)\").Each(func(i int, selection *goquery.Selection) {\r\n\t\ttitle := selection.Find(\".title a\").Text()\r\n\t\tprice := selection.Find(\".detail .detail-r\").Find(\".colf8\").Text()\r\n\t\tkilometer := selection.Find(\".detail .detail-l\").Find(\"p\").Eq(0).Text()\r\n\t\tyear := selection.Find(\".detail .detail-l\").Find(\"p\").Eq(1).Text()\r\n\r\n\t\tkilometer = strings.Join(compileNumber.FindAllString(kilometer, -1), \"\")\r\n\t\tyear = strings.Join(compileNumber.FindAllString(strings.TrimSpace(year), -1), \"\")\r\n\t\tpriceS, _ := strconv.ParseFloat(price, 64)\r\n\t\tkilometerS, _ := strconv.ParseFloat(kilometer, 64)\r\n\t\tyearS, _ := strconv.Atoi(year)\r\n\r\n\t\tcars = append(cars, QcCar{\r\n\t\t\tCityName: cityName,\r\n\t\t\tTitle: title,\r\n\t\t\tPrice: priceS,\r\n\t\t\tKilometer: kilometerS,\r\n\t\t\tYear: yearS,\r\n\t\t})\r\n\t})\r\n\r\n\treturn cars\r\n}\r\n```\r\n\r\n## 数据\r\n\r\n![image](https://i.loli.net/2018/03/31/5abf1d8042196.png)\r\n\r\n![image](https://i.loli.net/2018/04/01/5abfbaa14b09c.png)\r\n\r\n在各城市的平均价格对比中，我们可以发现北上广深里的北京、上海、深圳都在榜单上，而近年势头较猛的杭州直接占领了榜首，且后几名都有一些距离\r\n\r\n而其他城市大致都是梯级下降的趋势，看来一线城市的二手车也是不便宜了，当然这只是均价\r\n\r\n![image](https://i.loli.net/2018/03/31/5abf1dbc665f2.png)\r\n\r\n我们可以看到价格和公里数的对比，上海、成都、郑州的等比差异是有点大，感觉有需求的话可以在价格和公里数上做一个衡量\r\n\r\n![image](https://i.loli.net/2018/03/31/5abf1e1434edc.png)\r\n\r\n这图有点儿有趣，粗略的统计了一下总公里数。在前几张图里，平均价格排名较高的统统没有出现在这里，反倒是呼和浩特、大庆、中山等出现在了榜首\r\n\r\n是否侧面反应了一线城市的车辆更新换代较快，而较后的城市的车辆倒是换代较慢，公里数基本都杠杠的\r\n\r\n![image](https://i.loli.net/2018/03/31/5abf1e4936640.png)\r\n\r\n通过对标题的分析，可以得知车辆产品库的命名基本都是品牌名称+自动/手动+XXXX款+属性，看标题就能知道个概况了","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-crawler%2Fcar-prices","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgo-crawler%2Fcar-prices","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-crawler%2Fcar-prices/lists"}