losetear
2017-05-05 10:26:31 +08:00
简单改了下,做了个按折扣排序:
#for i in range(5):
url = URL_TEMPLATE % (20)
data = json.loads(requests.get(url).text)
shops = list({})
for restaurant in data:
id = restaurant["id"]
name = restaurant["name"]
for activity in restaurant["activities"]:
if "type" in activity and activity["type"] == 102: # 102 表示满减
tips = activity["tips"]
flag = True
lastzk = 999
for couple in zip(*[iter(re.findall(r'\d+', tips))]*2):
x = float(couple[0])
y = float(couple[1])
zk = (x-y)/x
if lastzk > zk:
lastzk = zk
shop = [id,name.replace(u'\u2219',u''),tips,lastzk]
#print("%d %s %s %s" % (id, name, zk,tips))
#print("%d %s %s %s" % (shop[0], shop[1], shop[2],shop[3]))
shops.append(shop)
shops.sort(key=lambda shop: shop[3])
for shop in shops:
print("%d %s %s %s" % (shop[0], shop[1], shop[2], shop[3]))