【问题标题】:How to compare the screen to a screenshot in Python如何将屏幕与 Python 中的屏幕截图进行比较
【发布时间】:2021-03-03 10:01:55
【问题描述】:

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

谁能帮我解决这个问题?

【问题讨论】:

    标签: python python-3.x image python-3.8 shopping


    【解决方案1】:

    好吧,您当然可以比较两个屏幕截图,使用 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
    

    祝你好运!

    【讨论】:

    • 我会将out-of-stock-text 的值设置为图像路径、要在页面上搜索的文本还是其他内容?抱歉,我以前从未使用过 Selenium。
    • 作为文本,selenium 或爬虫在显示页面的源中搜索。我猜 Selenium 是你所需要的,因为之后你必须填写搜索栏、点击按钮等……你会在网上找到很棒的教程,这就是我学习使用它的方法。编码愉快!
    猜你喜欢
    • 2017-08-07
    • 2016-09-05
    • 2012-12-26
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    相关资源
    最近更新 更多