首页 > 解决方案 > 如何将屏幕与 Python 中的屏幕截图进行比较

问题描述

我正在制作一个 Python 程序,它可以打开Target并购买用户输入的商品。如果该项目不可用,则程序不执行任何操作并在控制台中打印一条消息。要检查该项目是否可用,我需要检查屏幕是否包含类似于我截取的屏幕截图的内容。屏幕截图如下所示。 
[截图][1]

谁能帮我这个?

标签: pythonpython-3.ximagepython-3.8shopping

解决方案


好吧,您当然可以比较两个屏幕截图,使用 matplotlib、PIL 或 opencv(甚至 numpy)等库,但我想最简单的方法是使用selenium(如果您自动导航)或 beautifulsoup4/scrapy。

就像是 :

from selenium import webdriver

browser = webdriver.Firefox()
url = "https://www.target.com/"
browser.get(url)

# navigate to find your product
out_of_stock_text = "put your stuff here"
if out_of_stock_text in browsers.page_source:
   # Do your stuff here

祝你好运 !


推荐阅读