
影刀RPA 小程序自动化微信小程序数据采集与操作实战作者林焱写在前写微信小程序只能在微信里打开既没有独立网址也没有桌面端是数据采集的难点。影刀对小程序有两条路一是通过微信PC端操控小程序界面二是用手机ADB操控微信App里的小程序。本文两种方式都讲教你根据场景选择。一、PC端微信小程序自动化微信桌面版Windows可以运行部分小程序对于有PC端小程序的场景直接用影刀的桌面应用/浏览器元素捕获操控。方案选择店群矩阵自动化突破运营极限微信PC版小程序基于Chromium内核渲染可以用影刀的网页元素捕获来操控判断标准 - 小程序内容能在微信PC版打开 → 用PC端方案 - 小程序只有手机版 → 用手机ADB方案 - 小程序有对应H5网页版 → 直接用浏览器自动化最稳PC端操控小程序界面importpyautoguiimporttimedefopen_wechat_miniprogram(mp_name): 通过微信PC端打开指定小程序 方式搜索或从最近使用中打开 # 确保微信窗口在前台importsubprocess subprocess.Popen([powershell,-Command,Add-Type -Name W -MemberDefinition [DllImport(\user32.dll\)]public static extern bool SetForegroundWindow(IntPtr h); -Namespace W; [W.W]::SetForegroundWindow((Get-Process WeChat).MainWindowHandle)])time.sleep(1)# 点击小程序图标或搜索mp_iconpyautogui.locateOnScreen(wechat_miniprogram_btn.png,confidence0.85)ifmp_icon:pyautogui.click(mp_icon)time.sleep(2)# 搜索小程序search_boxpyautogui.locateOnScreen(wechat_search_box.png,confidence0.8)ifsearch_box:pyautogui.click(search_box)pyautogui.typewrite(mp_name,interval0.05)pyautogui.press(enter)time.sleep(2)二、手机端小程序ADB自动化打开微信小程序importsubprocessimporttimedefrun_adb(cmd,device_idNone):prefixfadb -s{device_id}ifdevice_idelseadb resultsubprocess.run(prefixcmd,shellTrue,capture_outputTrue,textTrue,encodingutf-8)returnresult.stdout.strip()deftap(x,y):run_adb(fshell input tap{x}{y})time.sleep(0.6)defscreenshot(save_pathmp_screen.png):run_adb(shell screencap -p /sdcard/screen.png)run_adb(fpull /sdcard/screen.png{save_path})defopen_miniprogram_by_search(mp_name):通过微信搜索打开小程序# 确保微信在前台run_adb(shell am start -n com.tencent.mm/.ui.LauncherUI)time.sleep(2)# 点击搜索框微信首页顶部W,H1080,2340# 根据实际分辨率调整tap(W*0.5,H*0.045)# 顶部搜索栏time.sleep(1)# 输入小程序名称run_adb(fshell am broadcast -a ADB_INPUT_TEXT --es msg {mp_name})time.sleep(0.5)run_adb(shell input keyevent 66)# 搜索time.sleep(2)# 在搜索结果中找小程序tab# 根据实际界面确定坐标screenshot(search_result.png)# 点击小程序tab通常在公众号旁边# tap(W * 0.3, H * 0.15) # 小程序tab位置小程序数据采集以某外卖小程序为例以某外卖小程序的商品列表为例演示采集流程fromPILimportImageimportpytesseractimportopenpyxlimportredefcrop_and_ocr(image_path,areaNone,langchi_sim):截取指定区域并OCR识别imgImage.open(image_path)ifarea:imgimg.crop(area)textpytesseract.image_to_string(img,langlang,config--psm 6)returntextdefcollect_food_items():采集外卖小程序商品信息W,H1080,2340all_items[]seen_namesset()scroll_count0max_scrolls30whilescroll_countmax_scrolls:# 截图screenshot(ffood_{scroll_count}.png)# 识别商品名称区域通常在屏幕左侧60%name_area(0,int(H*0.2),int(W*0.6),int(H*0.85))names_textcrop_and_ocr(ffood_{scroll_count}.png,name_area)# 识别价格区域右侧price_area(int(W*0.6),int(H*0.2),W,int(H*0.85))prices_textcrop_and_ocr(ffood_{scroll_count}.png,price_area)# 解析商品名和价格name_lines[l.strip()forlinnames_text.split(\n)ifl.strip()andlen(l.strip())1]price_matchesre.findall(r¥?(\d\.?\d*),prices_text)new_count0fori,nameinenumerate(name_lines):ifnamenotinseen_namesandlen(name)2:seen_names.add(name)priceprice_matches[i]ifilen(price_matches)elseall_items.append({商品名称:name,价格:price})new_count1print(f第{scroll_count1}次截图识别{len(name_lines)}个商品新增{new_count}个累计{len(all_items)}个)ifnew_count0:break# 向上滑动run_adb(fshell input swipe 540 1600 540 700 500)time.sleep(1.2)scroll_count1# 保存wbopenpyxl.Workbook()wswb.active ws.append([商品名称,价格])foriteminall_items:ws.append([item[商品名称],item[价格]])wb.save(外卖商品列表.xlsx)print(f采集完成{len(all_items)}个商品)returnall_items三、小程序登录态维持很多小程序需要登录才能采集数据手动登录一次后可以维持登录态defcheck_login_status(screenshot_path):检查小程序是否需要登录imgImage.open(screenshot_path)textpytesseract.image_to_string(img,langchi_sim)login_keywords[登录,授权,微信登录,手机号登录]forkeywordinlogin_keywords:ifkeywordintext:returnFalse# 需要登录returnTrue# 已登录defhandle_login_popup():处理小程序的微信授权弹窗W,H1080,2340# 点击允许按钮通常在底部allow_btnpyautogui.locateOnScreen(wechat_allow_btn.png,confidence0.85)ifallow_btn:tap(allow_btn.leftallow_btn.width//2,allow_btn.topallow_btn.height//2)returnTrue# 如果找不到图片尝试坐标点击# 微信授权弹窗允许按钮通常在屏幕下方60%处tap(W*0.7,H*0.65)returnTrue四、实战案例电商小程序商品价格监控temu店群自动化报活动案例每天定时采集某电商小程序的指定商品价格importdatetimedefmonitor_product_price(product_name,target_priceNone):监控指定商品价格低于目标价时通知# 打开小程序搜索商品open_miniprogram_by_search(某电商小程序)time.sleep(3)# 搜索商品tap(540,150)# 小程序内搜索框time.sleep(0.5)run_adb(fshell am broadcast -a ADB_INPUT_TEXT --es msg {product_name})run_adb(shell input keyevent 66)time.sleep(2)# 截图采集价格screenshot(product_search.png)imgImage.open(product_search.png)textpytesseract.image_to_string(img,langchi_sim)# 提取价格pricesre.findall(r¥(\d\.?\d*),text)ifprices:current_pricefloat(prices[0])todaydatetime.date.today().isoformat()print(f{today}{product_name}当前价格¥{current_price})# 记录价格历史withopen(price_history.csv,a,encodingutf-8)asf:f.write(f{today},{product_name},{current_price}\n)# 价格低于目标时通知iftarget_priceandcurrent_pricetarget_price:# 通过影刀发送通知邮件/微信/钉钉print(f 降价通知{product_name}当前价格¥{current_price}已低于目标价¥{target_price})returnTruereturnFalse# 每天9点运行监控monitor_product_price(某品牌耳机,target_price299.0)五、踩坑记录坑1小程序启动慢小程序首次启动需要下载代码包可能需要5-10秒。不要在启动后立即操作加足够的等待时间time.sleep(5)。坑2小程序版本更新导致界面变化小程序更新比App频繁今天截好的坐标明天可能就变了。建议用图像识别替代固定坐标或用OCR根据文字位置动态计算坐标。坑3微信检测自动化操作微信对模拟点击有一定的检测能力连续快速操作可能触发风控。加随机延时、模拟人工操作节奏单次会话操作不要太多。坑4截图中有弹窗遮挡内容红包提醒、消息通知等浮层会遮挡小程序内容。每次截图前先检查并关闭可能的弹窗。总结小程序自动化的核心挑战没有标准元素可以捕获只能依赖截图OCR 坐标操作。关键是做好坐标维护存配置文件、加充足等待时间、OCR后处理清洗数据。署名林焱