python爬取全国二手车数据,看看有没有可以捡漏的!
絮叨一下
话说,最近从湖北开始汽车价格战,全国人民都跑到湖北买车去了,搞得人家都出政策限制外地人购买了。
不过12W的C6是真香呐,二十多万的C6一身毛病,12W的C6毛病在我!
这波价格战直接蔓延到全国,全国的二手车商已原地哭晕。
今天我们就用Python来获取一下全国二手车数据,看看二手车有没有降价的。
python 3.8 pycharm 2022.3专业版 requests >>> pip install requests parsel >>> pip install parsel
- 发送请求
- 获取数据
- 解析数据
- 保存数据
- 发送请求
for page in range(1, 101): url = f'https://www.***.com/china/a0_0msdgscncgpi1ltocsp{page}exx0/' response = requests.get(url=url, headers=headers, proxies=get_proxies())
- 获取数据
html_data = response.text
- 解析数据
select = parsel.Selector(html_data) detail_url_list = select.xpath("//ul[@class='viewlist_ul']/li/a[@class='carinfo']/@href").getall() # Python学习交流群:872937351 for detail_url in detail_url_list[:-1]: if detail_url[1] == '/': detail_url = 'https:' + detail_url else: detail_url = 'https://www.**.com' + detail_url detail_html = requests.get(detail_url, headers=headers, proxies=get_proxies()).text detail_select = parsel.Selector(detail_html) title = detail_select.xpath("string(//h3[@class='car-brand-name'])").get("").strip() licheng = detail_select.xpath("//ul[@class='brand-unit-item fn-clear']/li[1]/h4/text()").get("").strip() shangpai = detail_select.xpath("//ul[@class='brand-unit-item fn-clear']/li[2]/h4/text()").get("").strip() pailiang = detail_select.xpath("//ul[@class='brand-unit-item fn-clear']/li[3]/h4/text()").get("").strip() suozaidi = detail_select.xpath("//ul[@class='brand-unit-item fn-clear']/li[4]/h4/text()").get("").strip() guobiao = detail_select.xpath("//ul[@class='brand-unit-item fn-clear']/li[5]/h4/text()").get("").strip() price = detail_select.xpath("string(//span[@id='overlayPrice'])").get("").strip() print(title, licheng, shangpai, pailiang, suozaidi, guobiao, price, detail_url)
- 保存数据
with open('汽车之家.csv', mode='a', encoding='utf-8', newline='') as f: csv_writer = csv.writer(f) csv_writer.writerow([card_name, cards_unit, price, img_url])
好了兄弟们,今天的分享就到这里结束了,咱们下次再见!
大家赶紧去试试,看看二手车价格吧,说不定还能遇上心动的下个单!