影刀RPA 自动化竞品监控系统:价格与产品变动实时追踪
2026/7/24 0:42:44 网站建设 项目流程

影刀RPA 自动化竞品监控系统:价格与产品变动实时追踪

作者:林焱


写在前面

竞品监控是运营和产品团队的日常工作,但靠人工每天盯竞品官网、电商页面极其低效。本文搭建一套完整的竞品监控系统:定时采集竞品数据 → 与历史数据对比 → 发现变化 → 推送告警,全程自动化。


一、监控哪些内容

拼多多店群自动化上架方案

# 竞品监控指标清单MONITOR_ITEMS={"价格监控":["单品价格","套餐价格","活动价格","折扣力度"],"产品变动":["新品上市","产品下架","产品规格变化","功能更新"],"营销活动":["促销活动","满减规则","优惠券","限时折扣"],![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/2c00f9b84b3141a38ce88fc64c6d6b1d.png#pic_center)"流量数据":["搜索排名","评价数量","销量变化"],"内容变化":["官网首页","产品介绍页","招聘页面(推断业务方向)"],}

二、电商竞品价格监控

importrequestsfrombs4importBeautifulSoupimportjsonimportsqlite3importdatetimeimporthashlibclassCompetitorMonitor:def__init__(self,db_path="competitor_data.db"):self.db_path=db_path self.conn=sqlite3.connect(db_path)self._init_db()def_init_db(self):"""初始化数据库"""cursor=self.conn.cursor()# 产品监控表cursor.execute(''' CREATE TABLE IF NOT EXISTS product_snapshots ( id INTEGER PRIMARY KEY AUTOINCREMENT, competitor TEXT NOT NULL, product_id TEXT NOT NULL, product_name TEXT, price REAL, original_price REAL, stock_status TEXT, rating REAL, review_count INTEGER, snapshot_hash TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )''')# 变化记录表cursor.execute(''' CREATE TABLE IF NOT EXISTS changes ( id INTEGER PRIMARY KEY AUTOINCREMENT, competitor TEXT, product_id TEXT, change_type TEXT, old_value TEXT, new_value TEXT, detected_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )''')self.conn.commit()defget_last_snapshot(self,competitor,product_id):"""获取最近一次的快照"""cursor=self.conn.cursor()cursor.execute(''' SELECT * FROM product_snapshots WHERE competitor=? AND product_id=? ORDER BY created_at DESC LIMIT 1 ''',(competitor,product_id))row=cursor.fetchone()ifrow:columns=[d[0]fordincursor.description]returndict(zip(columns,row))returnNonedefsave_snapshot(self,competitor,product_id,data):"""保存当前快照"""snapshot_content=json.dumps(data,sort_keys=True)snapshot_hash=hashlib.md5(snapshot_content.encode()).hexdigest()cursor=self.conn.cursor()cursor.execute(''' INSERT INTO product_snapshots (competitor, product_id, product_name, price, original_price, stock_status, rating, review_count, snapshot_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ''',(competitor,product_id,data.get('product_name'),data.get('price'),data.get('original_price'),data.get('stock_status'),data.get('rating'),data.get('review_count'),snapshot_hash))self.conn.commit()returnsnapshot_hashdefdetect_changes(self,competitor,product_id,old_data,new_data):"""检测数据变化并记录"""changes=[]# 价格变化检测old_price=old_data.get('price')new_price=new_data.get('price')ifold_priceandnew_priceandabs(new_price-old_price)/old_price>0.01:# 1%以上变化change_pct=(new_price-old_price)/old_price*100change={"type":"price_change","description":f"价格变化:{old_price}{new_price}{change_pct:+.1f}%)","severity":"high"ifabs(change_pct)>10else"medium"}changes.append(change)cursor=self.conn.cursor()cursor.execute(''' INSERT INTO changes (competitor, product_id, change_type, old_value, new_value) VALUES (?, ?, ?, ?, ?) ''',(competitor,product_id,"price",str(old_price),str(new_price)))self.conn.commit()# 缺货检测old_stock=old_data.get('stock_status')new_stock=new_data.get('stock_status')ifold_stock!=new_stock:changes.append({"type":"stock_change","description":f"库存状态变化:{old_stock}{new_stock}","severity":"medium"})returnchanges

三、实战:京东商品价格监控

defscrape_jd_product(product_id,session=None):""" 采集京东商品信息 product_id: 商品ID(URL中的数字) """ifsessionisNone:session=requests.Session()session.headers.update({"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36","Referer":"https://www.jd.com/"})url=f"https://item.jd.com/{product_id}.html"try:resp=session.get(url,timeout=10)soup=BeautifulSoup(resp.text,'html.parser')# 提取商品名name_tag=soup.select_one('.sku-name')product_name=name_tag.text.strip()ifname_tagelse""# 价格需要通过API获取(京东实时价格是异步加载的)price_url=f"https://p.3.cn/prices/mgets?skuid=J_{product_id}"price_resp=session.get(price_url,timeout=5)price_data=json.loads(price_resp.text.strip('/**/'))current_price=float(price_data[0].get('p',0))ifprice_dataelse0original_price=float(price_data[0].get('op',current_price))ifprice_dataelsecurrent_price# 评价数(需要额外接口)comment_url=f"https://club.jd.com/comment/productCommentSummaries.action?referenceIds={product_id}"comment_resp=session.get(comment_url,timeout=5)comment_data=comment_resp.json()review_count=comment_data.get('CommentsCount',[{}])[0].get('ShowCount',0)return{"product_name":product_name,"price":current_price,"original_price":original_price,"review_count":review_count,"url":url}exceptExceptionase:print(f"京东采集失败{product_id}{e}")returnNonedefmonitor_competitor_products():"""监控竞品商品列表"""# 竞品商品配置COMPETITOR_PRODUCTS=[{"competitor":"竞品A","platform":"jd","product_id":"100012345678"},{"competitor":"竞品A","platform":"jd","product_id":"100023456789"},{"competitor":"竞品B","platform":"jd","product_id":"100034567890"},]monitor=CompetitorMonitor()session=requests.Session()session.headers.update({"User-Agent":"Mozilla/5.0 ..."})all_changes=[]forproduct_configinCOMPETITOR_PRODUCTS:competitor=product_config["competitor"]product_id=product_config["product_id"]# 采集当前数据current_data=scrape_jd_product(product_id,session)ifnotcurrent_data:continueprint(f"{competitor}-{current_data['product_name'][:20]}: ¥{current_data['price']}")# 获取历史数据,检测变化last_snapshot=monitor.get_last_snapshot(competitor,product_id)iflast_snapshot:changes=monitor.detect_changes(competitor,product_id,last_snapshot,current_data)ifchanges:forchangeinchanges:change["competitor"]=competitor change["product_name"]=current_data.get("product_name","")[:30]change["current_price"]=current_data.get("price")all_changes.append(change)print(f" ⚡ 变化检测:{change['description']}")# 保存新快照monitor.save_snapshot(competitor,product_id,current_data)importtime time.sleep(1)# 采集间隔returnall_changes

四、官网内容变化监控

defmonitor_website_content(urls,keyword_filters=None):""" 监控竞品官网内容变化 通过对比页面内容hash来检测变化 """session=requests.Session()session.headers.update({"User-Agent":"Mozilla/5.0 ..."})changes=[]snapshots_file="website_snapshots.json"# 读取历史快照try:withopen(snapshots_file,'r',encoding='utf-8')asf:old_snapshots=json.load(f)except:old_snapshots={}new_snapshots={}forurl_configinurls:url=url_config["url"]name=url_config.get("name",url)try:resp=session.get(url,timeout=15)soup=BeautifulSoup(resp.text,'html.parser')# 提取正文文字(排除导航、页脚等噪声)fortaginsoup.find_all(['nav','footer','script','style','header']):tag.decompose()page_text=soup.get_text(separator='\n',strip=True)# 过滤关键词(只关注包含特定词汇的段落)ifkeyword_filters:relevant_lines=[lineforlineinpage_text.split('\n')ifany(kwinlineforkwinkeyword_filters)]page_text='\n'.join(relevant_lines)content_hash=hashlib.md5(page_text.encode()).hexdigest()new_snapshots[url]={"hash":content_hash,"text_preview":page_text[:500],"updated_at":datetime.datetime.now().isoformat()}# 与历史对比ifurlinold_snapshots:ifold_snapshots[url]["hash"]!=content_hash:changes.append({"type":"content_change","name":name,"url":url,"description":f"官网内容发生变化:{name}","severity":"medium"})print(f"⚡ 内容变化:{name}")exceptExceptionase:print(f"采集失败:{url}-{e}")# 保存新快照withopen(snapshots_file,'w',encoding='utf-8')asf:json.dump(new_snapshots,f,ensure_ascii=False,indent=2)returnchanges

五、变化告警推送

defsend_change_alert(all_changes,webhook_url):"""汇总并推送变化告警"""ifnotall_changes:print("本次监控无变化")returnhigh_changes=[cforcinall_changesifc.get("severity")=="high"]medium_changes=[cforcinall_changesifc.get("severity")=="medium"]content=f"## 🔍 竞品监控告警\n\n"content+=f"**监控时间**:{datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}\n"content+=f"**变化总数**:{len(all_changes)}项(高优先{len(high_changes)}项)\n\n"ifhigh_changes:content+="### ⚠️ 高优先级变化\n"forchangeinhigh_changes:content+=f"- **{change.get('competitor','')}** |{change.get('description','')}\n"content+="\n"ifmedium_changes:content+="### ℹ️ 一般变化\n"forchangeinmedium_changes[:5]:content+=f"-{change.get('description','')}\n"iflen(medium_changes)>5:content+=f"- 等{len(medium_changes)-5}项更多...\n"[video(video-spylf4IR-1784822642246)(type-csdn)(url-https://live.csdn.net/v/embed/524993)(image-https://v-blog.csdnimg.cn/asset/a547123d88ad712dccba346c9217e237/cover/Cover0.jpg)(title-TEMU店群如何管理运营?)]importrequests payload={"msgtype":"markdown","markdown":{"content":content}}requests.post(webhook_url,json=payload)defrun_full_monitoring():"""完整的竞品监控流程"""print(f"开始竞品监控:{datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}")# 1. 电商价格监控price_changes=monitor_competitor_products()# 2. 官网内容监控competitor_urls=[{"name":"竞品A官网","url":"https://competitor-a.com/products"},{"name":"竞品B定价页","url":"https://competitor-b.com/pricing"},]content_changes=monitor_website_content(competitor_urls,keyword_filters=["价格","新品","发布"])# 3. 汇总告警all_changes=price_changes+content_changes WEBHOOK="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的key"send_change_alert(all_changes,WEBHOOK)run_full_monitoring()

六、踩坑记录

坑1:电商价格异步加载
京东、天猫的价格不在页面HTML里,是JS动态加载的。不能直接抓HTML,要找价格API接口(通过抓包或查看网络请求)单独请求。

坑2:官网内容频繁细微变化
广告位、时间戳、随机内容会导致每次都检测到"变化"。解决方案:在提取文字时过滤掉这些动态元素,只关注产品相关内容区域。

坑3:IP被封
频繁采集竞品网站会触发封IP。关键对策:合理设置采集间隔(每个URL至少间隔1分钟)、用代理IP轮换、User-Agent随机化。

坑4:需要登录才能看到价格
会员价、登录后价格需要维护一个已登录的Session(cookies)。用Playwright登录一次后导出cookies,在requests.Session里设置cookies使用。


总结

竞品监控系统的核心:定时采集+历史对比+即时告警。价格监控优先用电商平台API接口(稳定),内容监控用hash对比(简单有效)。告警要有优先级,不然信息轰炸会被忽略。

署名:林焱

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询