马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import requests
from lxml import etree
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36',
'Cookie': 'antipas=38p542Z93260M92A5S55D; uuid=2bc4d66a-282f-4d2e-8202-3600830a5c79; clueSourceCode=%2A%2300; user_city_id=45; Hm_lvt_936a6d5df3f3d309bda39e92da3dd52f=1600584882; ganji_uuid=5704297242784487577783; sessionid=b8d3e268-ef08-47fb-eef2-98bb4bf0ba54; lg=1; lng_lat=104.078574_30.481192; gps_type=1; close_finance_popup=2020-09-20; cainfo=%7B%22ca_a%22%3A%22-%22%2C%22ca_b%22%3A%22-%22%2C%22ca_s%22%3A%22seo_baidu%22%2C%22ca_n%22%3A%22default%22%2C%22ca_medium%22%3A%22-%22%2C%22ca_term%22%3A%22-%22%2C%22ca_content%22%3A%22-%22%2C%22ca_campaign%22%3A%22-%22%2C%22ca_kw%22%3A%22-%22%2C%22ca_i%22%3A%22-%22%2C%22scode%22%3A%22-%22%2C%22keyword%22%3A%22-%22%2C%22ca_keywordid%22%3A%22-%22%2C%22display_finance_flag%22%3A%22-%22%2C%22platform%22%3A%221%22%2C%22version%22%3A1%2C%22client_ab%22%3A%22-%22%2C%22guid%22%3A%222bc4d66a-282f-4d2e-8202-3600830a5c79%22%2C%22ca_city%22%3A%22cd%22%2C%22sessionid%22%3A%22b8d3e268-ef08-47fb-eef2-98bb4bf0ba54%22%7D; preTime=%7B%22last%22%3A1600584999%2C%22this%22%3A1600584880%2C%22pre%22%3A1600584880%7D; cityDomain=linfen; _gl_tracker=%7B%22ca_source%22%3A%22-%22%2C%22ca_name%22%3A%22-%22%2C%22ca_kw%22%3A%22-%22%2C%22ca_id%22%3A%22-%22%2C%22ca_s%22%3A%22self%22%2C%22ca_n%22%3A%22-%22%2C%22ca_i%22%3A%22-%22%2C%22sid%22%3A53804476135%7D; Hm_lpvt_936a6d5df3f3d309bda39e92da3dd52f=1600585005',
'Referer':'https://www.guazi.com/cd/buy/'
}
url_detail='https://www.guazi.com/cd/buy/o2'
res=requests.get(url=url_detail,headers=headers)
html=etree.HTML(res.text)
ul=html.xpath('/html/body/div[6]/ul')[0]
kong=[]
for li in ul:
if li.xpath('./a/@title') !=kong:
title=li.xpath('./a/@title')[0]
else:
title="待定"
if li.xpath('./a/div[2]/p/text()') !=kong:
price=li.xpath('./a/div[2]/p/text()')[0]
else:
price="未知"
if li.xpath('./a/div[1]/text()[1]') !=kong:
year=li.xpath('./a/div[1]/text()[1]')[0]
else:
year="未知"
if li.xpath('./a/div[1]/text()[2]')!=kong:
km=li.xpath('./a/div[1]/text()[2]')[0]
else:
km="未知"
if len(li.xpath('./a/div[2]/em/text()') )==0:
old_price="0"
else:
old_price=li.xpath('./a/div[2]/em/text()')[0]
print(title,price,old_price,year,km)
如上, 使用了很多if---else 语句。看怎么样优化下一下。
之所以这样,是因为,偶尔某些项目找不到会报错(out of range)
谢谢
可以试着这么写,会减少点代码 for li in ul:
title = li.xpath('./a/@title')
title = title[0] if len(title) > 0 else "待定"
price = li.xpath('./a/div[2]/p/text()')
price = price[0] if len(price) > 0 else "未知"
year = li.xpath('./a/div[1]/text()[1]')
year = year[0] if len(year) > 0 else "未知"
km = li.xpath('./a/div[1]/text()[2]')
km = km[0] if len(km) > 0 else "未知"
old_price = li.xpath('./a/div[2]/em/text()')
old_price = old_price[0] if len(old_price) > 0 else "0"
print(title, price, old_price, year, km)
|