配置监控参数
KEYWORD = "二手 MacBook"
PRICE_LIMIT = 5000
CHECK_INTERVAL = 60 # 检测间隔秒数
SEEN_IDS = set() # 已见过的商品 ID 集合
async def fetch_xianyu_items(session, keyword):
"""
模拟请求闲鱼搜索接口
注意:实际使用需替换为真实的 H5 接口 URL 及完整的 Headers/Cookie/Token
"""
此处 URL 仅为示例结构,真实环境需通过抓包获取最新接口
url = "https://s.2.taobao.com/list"
params = {
"q": keyword,
"search_type": "item",
"page": "1"
}
headers = {
"User-Agent": "Mozilla/5.0 (Linux; Android 10; ...) AppleWebKit/537.36",
"Cookie": "your_valid_cookie_here", # 必须包含有效 Cookie
"x-sig": "your_signature_here" # 部分接口需要签名
}
try:
async with session.get(url, params=params, headers=headers) as resp:
if resp.status == 200:
data = await resp.json()
return data.get('data', {}).get('items', [])
else:
print(f"请求失败,状态码:{resp.status}")
return []
except Exception as e:
print(f"发生异常:{e}")
return []
def process_items(items):
"""处理商品数据并筛选"""
new_found = False
for item in items:
item_id = item.get('id')
price = float(item.get('price', 999999))
title = item.get('title', '')
# 判断是否为新商品且符合价格要求
if item_id not in SEEN_IDS and price <= PRICE_LIMIT:
SEEN_IDS.add(item_id)
print(f"[捡漏提醒] 发现新品!标题:{title} | 价格:{price} 元")
# 此处可集成发送通知的代码 (如调用钉钉/微信机器人 API)
new_found = True
if not new_found:
print(f"[{time.strftime('%H:%M:%S')}] 本轮未发现符合条件的新商品")
async def monitor_loop():
async with aiohttp.ClientSession() as session:
while True:
print(f"开始第 {len(SEEN_IDS)} 次扫描...")
items = await fetch_xianyu_items(session, KEYWORD)
if items:
process_items(items)
await asyncio.sleep(CHECK_INTERVAL)
if name == "main ":
运行监控循环
asyncio.run(monitor_loop())
