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

Benjamin Flores Vega

「今晚哪家附近披薩店真正又好又快?」

UberEats 並沒有真正回答這個問題。它會依據相關性、促銷活動以及演算法的隨機排序——而不是依據「評分與外送速度的最佳組合」,而這正是我在週二晚上八點真正想要的。

因此我建立了一個小型工具,來正確回答這個問題,使用的工具是 Apify 上的 UberEats Stores Search by Location and Keyword actor,以及約 40 行 Python 程式碼。

想法

根據給定的地址,抓取符合關鍵字的所有店家——名稱、評分、預估外送時間——然後自行排序,而不是依賴應用程式的預設排序。一家 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())

每家店家都會回傳 estimated_time_to_delivery,格式為 "20 min"。解析它並為每家店家評分——評分很重要,但外送時間越長,評分應該越低:

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', '?')}")

就這樣——一個真正平衡「好」與「快」的排名前五名,而不是應用程式隨意決定的順序。

把它做成 CLI

把它包裝成可以針對任何地址/關鍵字組合執行的工具:

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 個結果)。

如果你將評分公式調整成更好的版本,我很樂意看到它——請在評論中分享。