Cover image for I Built a "Fastest Pizza Near Me" Tool with the UberEats API and Python

Benjamin Flores Vega

「今晚哪家附近的披萨店又好吃快?」

UberEats 并没有真正回答这个问题。它按相关性、促销和算法的喜好排序——而不是按「评分与配送速度的最佳组合」排序,而这才是我在周二晚上 8 点真正想要的。

于是我用 Apify 上的 UberEats Stores Search by Location and Keyword Actor 和大约 40 行 Python 代码,打造了一个能正确回答这个问题的小工具。

思路

在给定地址拉取匹配关键词的所有店铺——名称、评分、预计配送时间——然后自己进行排名,而不是依赖 App 默认的排序。一家评分 4.8、配送 45 分钟的店铺不一定比评分 4.5、20 分钟送达的店铺更好。

操作步骤

安装客户端:

pip install apify-client

抓取数据:

from apify_client import ApifyClient

client = ApifyClient("YOUR_APIFY_TOKEN")

run_input = {
    "locations": ["85 Marsh St, Newark, NJ 07114, USA"],
    "search_keyword": "pizza",
    "max_search_results": 30,
}

run = client.actor("datacach/ubereats-stores-search-by-location-and-keyword").call(run_input=run_input)
items = list(client.dataset(run["defaultDatasetId"]).iterate_items())

每个店铺都会返回一个形如 "20 min"estimated_time_to_delivery 字符串。解析并为每个店铺打分——评分很重要,但配送时间越长,分数越低:

import re

def parse_minutes(text):
    match = re.search(r"\d+", text or "")
    return int(match.group()) if match else 60  # unknown time = penalize heavily

def score(store):
    rating = store.get("rating") or 0
    minutes = parse_minutes(store.get("estimated_time_to_delivery"))
    return rating - 0.03 * minutes  # tune this weight to taste

ranked = sorted(items, key=score, reverse=True)

for store in ranked[:5]:
    print(f"{store['name']:<30} ★{store.get('rating', '?'):<5} {store.get('estimated_time_to_delivery', '?')}")

就这样——一个真正兼顾「好吃」和「快」的排名前 5 列表,而不是 App 决定展示的任意顺序。

做成命令行工具

封装一下,让你可以针对任意地址/关键词组合运行:

import sys

if __name__ == "__main__":
    address, keyword = sys.argv[1], sys.argv[2]
    # ...call the actor with {"locations": [address], "search_keyword": keyword}...
python fastest_near_me.py "123 Main St, Newark, NJ, USA" "sushi"

扩展思路

  • 对比不同菜系:在同一地址分别运行「pizza」、「sushi」和「burgers」,看看今晚到底哪种菜系最快。
  • 对比不同社区:在几个不同地址运行同一关键词,找出城里哪个区域的外卖选项最好。
  • 调整评分公式:把 rating_count 也纳入权重——3 条评论的 4.8 分,远不如 500 条评论的 4.5 分可靠。

试用

如果你想先在自己的地址上试用,再决定是否扩容,这里提供免费方案(每次运行 1 个地点、10 条结果)。

如果你把评分公式改进得更好了,我很想看看——欢迎在评论区分享。