import requests as r
from lxml import etree
import re,os,random,time
url='http://xxx.com/xx'
res=r.get(url)
html=etree.HTML(res.text)
title=html.xpath('//title/text()')
lis=html.xpath('//ul[@class="viewlist_ul"]/li[@name="lazyloadcpc"]')
print('共得到车个数:',len(lis))
cars=[]
for li in lis:
name=li.xpath('.//h4[@class="card-name"]/text()')[0] #【从当前节点取数据要用】 .//
zonghe=li.xpath('.//p[@class="cards-unit"]/text()')[0]
zh=re.findall(r'(.*?)/(.*?)/(.*?)/(.*)',zonghe)
licheng=zh[0][0].replace('万公里','')
year=zh[0][1]
address=zh[0][2]
shangjia=zh[0][3]
price=li.xpath('.//span[@class="pirce"]//text()')
price=price[-1] if price[-2]=='抢购价' else price[-2] #如果内部有抢购价则价格取-1,否则取-2
price2=li.xpath('.//div[@class="cards-price-box"]/s/text()')[0]
tag=li.xpath('.//div[@class="cards-price-box"]/span[@class="tags"]/i/text()')
tag=tag[0] if tag else '' #如果tag存在内容则=tag[0],否则=''
cars.append({'车名':name,'里程':licheng,'年份':year,'地址':address,'商家':shangjia,
'价格':price,'原价':price2,'其它':tag
})
print(cars)