基于Selenium+Python实现自动解数独,秒变最强大脑

news2024/11/15 19:45:25

前言

在本文中,我将向大家介绍如何使用Python编写一个数独脚本,并实现模拟完成数独游戏的脚本。数独是一种经典的逻辑游戏,它可以帮助我们锻炼逻辑思维能力。通过编写数独脚本,我们可以使用Python语言解决数独问题,从而更好地理解编程和算法。

效果

 

环境使用

  • python 3.9
  • pycharm

模块使用

  • selenium

模块介绍

  • requests

        requests是一个很实用的Python HTTP客户端库,爬虫和测试服务器响应数据时经常会用到,requests是Python语言的第三方的库,专门用于发送HTTP请求,使用起来比urllib简洁很多。

  • parsel

        parsel是一个python的第三方库,相当于css选择器+xpath+re。

parsel由scrapy团队开发,是将scrapy中的parsel独立抽取出来的,可以轻松解析html,xml内容,获取需要的数据。

相比于BeautifulSoup,xpath,parsel效率更高,使用更简单。

  • re

        re模块是python独有的匹配字符串的模块,该模块中提供的很多功能是基于正则表达式实现的,而正则表达式是对字符串进行模糊匹配,提取自己需要的字符串部分,他对所有的语言都通用。

  • os

        os 就是 “operating system” 的缩写,顾名思义,os模块提供的就是各种 Python 程序与操作系统进行交互的接口。通过使用 os 模块,一方面可以方便地与操作系统进行交互,另一方面也可以极大增强代码的可移植性。

  • csv

        它是一种文件格式,一般也被叫做逗号分隔值文件,可以使用 Excel 软件或者文本文档打开 。其中数据字段用半角逗号间隔(也可以使用其它字符),使用 Excel 打开时,逗号会被转换为分隔符。csv 文件是以纯文本形式存储了表格数据,并且在兼容各个操作系统。

模块安装问题:

  • 如果安装python第三方模块:

win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车

在pycharm中点击Terminal(终端) 输入安装命令

  • 安装失败原因:

  • 失败一: pip 不是内部命令

                解决方法: 设置环境变量

  • 失败二: 出现大量报红 (read time out)

                解决方法: 因为是网络链接超时, 需要切换镜像源

   

    清华:https://pypi.tuna.tsinghua.edu.cn/simple
    阿里云:https://mirrors.aliyun.com/pypi/simple/
    中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
    华中理工大学:https://pypi.hustunique.com/
    山东理工大学:https://pypi.sdutlinux.org/
    豆瓣:https://pypi.douban.com/simple/
    例如:pip3 install -i https://pypi.doubanio.com/simple/ 模块名
  • 失败三: cmd里面显示已经安装过了, 或者安装成功了, 但是在pycharm里面还是无法导入

                解决方法: 可能安装了多个python版本 (anaconda 或者 python 安装一个即可) 卸载一个就好,或者你pycharm里面python解释器没有设置好。

代码实现

这个是不是很厉害,实际上很简单,接下来,我一步一步教大家如何完成这样的脚本。

获取数独数据

我们首先进入游戏的网页,把网页中的九宫格的数字获取下来。在线数独游戏官网。

接下来,我们使用开发者工具,获取这些数字的CSS或者XPath,因为我们要用selenium获取下来,这里我们要注意,我们这里还要注意数字的顺序,否则,会影响我们后面解数独。我们先看看网页源代码吧。

我们会发现,每九个数放在了一个class="sz?"里面,其中每个数都有自己对应的坐标,比如说,第一行第一列的数就是c_0_0,第一个0表示列数,第二个0表示行数,这个对我们后期代码的编写有很大的帮助。下面,我们先写一个获取第一行第一列的数的代码。

    c_0_0 = driver.find_element(By.CSS_SELECTOR, '#c_0_0').text
    if c_0_0 == '':
        c_0_0 = 0
        list1.append(c_0_0)
    else:
        list1.append(int(c_0_0))

这段代码使用了Selenium库中的find_element方法来查找一个元素,并获取该元素的文本。然后,它检查这个文本是否为空,如果为空,则将空文本转换为整数0,并将其添加到一个名为list1的列表中。如果文本不为空,则将其转换为整数,并添加到list1列表中。至于为什么要放到列表里面呢,是因为后面我们解数独要要用到,其他数的代码都一样,我这里就直接展示了。

list1 = []
    # (0,0)
    c_0_0 = driver.find_element(By.CSS_SELECTOR, '#c_0_0').text
    if c_0_0 == '':
        c_0_0 = 0
        list1.append(c_0_0)
    else:
        list1.append(int(c_0_0))
    # (1,0)
    c_1_0 = driver.find_element(By.CSS_SELECTOR, '#c_1_0').text
    if c_1_0 == '':
        c_1_0 = 0
        list1.append(c_1_0)
    else:
        list1.append(int(c_1_0))
    # (2,0)
    c_2_0 = driver.find_element(By.CSS_SELECTOR, '#c_2_0').text
    if c_2_0 == '':
        c_2_0 = 0
        list1.append(c_2_0)
    else:
        list1.append(int(c_2_0))
    # (3,0)
    c_3_0 = driver.find_element(By.CSS_SELECTOR, '#c_3_0').text
    if c_3_0 == '':
        c_3_0 = 0
        list1.append(c_3_0)
    else:
        list1.append(int(c_3_0))
    # (4,0)
    c_4_0 = driver.find_element(By.CSS_SELECTOR, '#c_4_0').text
    if c_4_0 == '':
        c_4_0 = 0
        list1.append(c_4_0)
    else:
        list1.append(int(c_4_0))
    # (5,0)
    c_5_0 = driver.find_element(By.CSS_SELECTOR, '#c_5_0').text
    if c_5_0 == '':
        c_5_0 = 0
        list1.append(c_5_0)
    else:
        list1.append(int(c_5_0))
    # (6,0)
    c_6_0 = driver.find_element(By.CSS_SELECTOR, '#c_6_0').text
    if c_6_0 == '':
        c_6_0 = 0
        list1.append(c_6_0)
    else:
        list1.append(int(c_6_0))
    # (7,0)
    c_7_0 = driver.find_element(By.CSS_SELECTOR, '#c_7_0').text
    if c_7_0 == '':
        c_7_0 = 0
        list1.append(c_7_0)
    else:
        list1.append(int(c_7_0))
    # (8,0)
    c_8_0 = driver.find_element(By.CSS_SELECTOR, '#c_8_0').text
    if c_8_0 == '':
        c_8_0 = 0
        list1.append(c_8_0)
    else:
        list1.append(int(c_8_0))
    list2 = []
    # (0,1)
    c_0_1 = driver.find_element(By.CSS_SELECTOR, '#c_0_1').text
    if c_0_1 == '':
        c_0_1 = 0
        list2.append(c_0_1)
    else:
        list2.append(int(c_0_1))
    # (1,1)
    c_1_1 = driver.find_element(By.CSS_SELECTOR, '#c_1_1').text
    if c_1_1 == '':
        c_1_1 = 0
        list2.append(c_1_1)
    else:
        list2.append(int(c_1_1))
    # (2,1)
    c_2_1 = driver.find_element(By.CSS_SELECTOR, '#c_2_1').text
    if c_2_1 == '':
        c_2_1 = 0
        list2.append(c_2_1)
    else:
        list2.append(int(c_2_1))
    # (3,1)
    c_3_1 = driver.find_element(By.CSS_SELECTOR, '#c_3_1').text
    if c_3_1 == '':
        c_3_1 = 0
        list2.append(c_3_1)
    else:
        list2.append(int(c_3_1))
    # (4,1)
    c_4_1 = driver.find_element(By.CSS_SELECTOR, '#c_4_1').text
    if c_4_1 == '':
        c_4_1 = 0
        list2.append(c_4_1)
    else:
        list2.append(int(c_4_1))
    # (5,1)
    c_5_1 = driver.find_element(By.CSS_SELECTOR, '#c_5_1').text
    if c_5_1 == '':
        c_5_1 = 0
        list2.append(c_5_1)
    else:
        list2.append(int(c_5_1))
    # (6,1)
    c_6_1 = driver.find_element(By.CSS_SELECTOR, '#c_6_1').text
    if c_6_1 == '':
        c_6_1 = 0
        list2.append(c_6_1)
    else:
        list2.append(int(c_6_1))
    # (7,1)
    c_7_1 = driver.find_element(By.CSS_SELECTOR, '#c_7_1').text
    if c_7_1 == '':
        c_7_1 = 0
        list2.append(c_7_1)
    else:
        list2.append(int(c_7_1))
    # (8,1)
    c_8_1 = driver.find_element(By.CSS_SELECTOR, '#c_8_1').text
    if c_8_1 == '':
        c_8_1 = 0
        list2.append(c_8_1)
    else:
        list2.append(int(c_8_1))
    list3 = []
    # (0,2)
    c_0_2 = driver.find_element(By.CSS_SELECTOR, '#c_0_2').text
    if c_0_2 == '':
        c_0_2 = 0
        list3.append(c_0_2)
    else:
        list3.append(int(c_0_2))
    # (1,2)
    c_1_2 = driver.find_element(By.CSS_SELECTOR, '#c_1_2').text
    if c_1_2 == '':
        c_1_2 = 0
        list3.append(c_1_2)
    else:
        list3.append(int(c_1_2))
    # (2,2)
    c_2_2 = driver.find_element(By.CSS_SELECTOR, '#c_2_2').text
    if c_2_2 == '':
        c_2_2 = 0
        list3.append(c_2_2)
    else:
        list3.append(int(c_2_2))
    # (3,2)
    c_3_2 = driver.find_element(By.CSS_SELECTOR, '#c_3_2').text
    if c_3_2 == '':
        c_3_2 = 0
        list3.append(c_3_2)
    else:
        list3.append(int(c_3_2))
    # (4,2)
    c_4_2 = driver.find_element(By.CSS_SELECTOR, '#c_4_2').text
    if c_4_2 == '':
        c_4_2 = 0
        list3.append(c_4_2)
    else:
        list3.append(int(c_4_2))
    # (5,2)
    c_5_2 = driver.find_element(By.CSS_SELECTOR, '#c_5_2').text
    if c_5_2 == '':
        c_5_2 = 0
        list3.append(c_5_2)
    else:
        list3.append(int(c_5_2))
    # (6,2)
    c_6_2 = driver.find_element(By.CSS_SELECTOR, '#c_6_2').text
    if c_6_2 == '':
        c_6_2 = 0
        list3.append(c_6_2)
    else:
        list3.append(int(c_6_2))
    # (7,2)
    c_7_2 = driver.find_element(By.CSS_SELECTOR, '#c_7_2').text
    if c_7_2 == '':
        c_7_2 = 0
        list3.append(c_7_2)
    else:
        list3.append(int(c_7_2))
    # (8,2)
    c_8_2 = driver.find_element(By.CSS_SELECTOR, '#c_8_2').text
    if c_8_2 == '':
        c_8_2 = 0
        list3.append(c_8_2)
    else:
        list3.append(int(c_8_2))
    list4 = []
    # (0,3)
    c_0_3 = driver.find_element(By.CSS_SELECTOR, '#c_0_3').text
    if c_0_3 == '':
        c_0_3 = 0
        list4.append(c_0_3)
    else:
        list4.append(int(c_0_3))
    # (1,3)
    c_1_3 = driver.find_element(By.CSS_SELECTOR, '#c_1_3').text
    if c_1_3 == '':
        c_1_3 = 0
        list4.append(c_1_3)
    else:
        list4.append(int(c_1_3))
    # (2,3)
    c_2_3 = driver.find_element(By.CSS_SELECTOR, '#c_2_3').text
    if c_2_3 == '':
        c_2_3 = 0
        list4.append(c_2_3)
    else:
        list4.append(int(c_2_3))
    # (3,3)
    c_3_3 = driver.find_element(By.CSS_SELECTOR, '#c_3_3').text
    if c_3_3 == '':
        c_3_3 = 0
        list4.append(c_3_3)
    else:
        list4.append(int(c_3_3))
        
    # (4,3)
    c_4_3 = driver.find_element(By.CSS_SELECTOR, '#c_4_3').text
    if c_4_3 == '':
        c_4_3 = 0
        list4.append(c_4_3)
    else:
        list4.append(int(c_4_3))
    # (5,3)
    c_5_3 = driver.find_element(By.CSS_SELECTOR, '#c_5_3').text
    if c_5_3 == '':
        c_5_3 = 0
        list4.append(c_5_3)
    else:
        list4.append(int(c_5_3))
    # (6,3)
    c_6_3 = driver.find_element(By.CSS_SELECTOR, '#c_6_3').text
    if c_6_3 == '':
        c_6_3 = 0
        list4.append(c_6_3)
    else:
        list4.append(int(c_6_3))
    # (7,3)
    c_7_3 = driver.find_element(By.CSS_SELECTOR, '#c_7_3').text
    if c_7_3 == '':
        c_7_3 = 0
        list4.append(c_7_3)
    else:
        list4.append(int(c_7_3))
    # (8,3)
    c_8_3 = driver.find_element(By.CSS_SELECTOR, '#c_8_3').text
    if c_8_3 == '':
        c_8_3 = 0
        list4.append(c_8_3)
    else:
        list4.append(int(c_8_3))
    list5 = []
    # (0,4)
    c_0_4 = driver.find_element(By.CSS_SELECTOR, '#c_0_4').text
    if c_0_4 == '':
        c_0_4 = 0
        list5.append(c_0_4)
    else:
        list5.append(int(c_0_4))
    # (1,4)
    c_1_4 = driver.find_element(By.CSS_SELECTOR, '#c_1_4').text
    if c_1_4 == '':
        c_1_4 = 0
        list5.append(c_1_4)
    else:
        list5.append(int(c_1_4))
    # (2,4)
    c_2_4 = driver.find_element(By.CSS_SELECTOR, '#c_2_4').text
    if c_2_4 == '':
        c_2_4 = 0
        list5.append(c_2_4)
    else:
        list5.append(int(c_2_4))
    # (3,4)
    c_3_4 = driver.find_element(By.CSS_SELECTOR, '#c_3_4').text
    if c_3_4 == '':
        c_3_4 = 0
        list5.append(c_3_4)
    else:
        list5.append(int(c_3_4))
    # (4,4)
    c_4_4 = driver.find_element(By.CSS_SELECTOR, '#c_4_4').text
    if c_4_4 == '':
        c_4_4 = 0
        list5.append(c_4_4)
    else:
        list5.append(int(c_4_4))
    # (5,4)
    c_5_4 = driver.find_element(By.CSS_SELECTOR, '#c_5_4').text
    if c_5_4 == '':
        c_5_4 = 0
        list5.append(c_5_4)
    else:
        list5.append(int(c_5_4))
    # (6,4)
    c_6_4 = driver.find_element(By.CSS_SELECTOR, '#c_6_4').text
    if c_6_4 == '':
        c_6_4 = 0
        list5.append(c_6_4)
    else:
        list5.append(int(c_6_4))
    # (7,4)
    c_7_4 = driver.find_element(By.CSS_SELECTOR, '#c_7_4').text
    if c_7_4 == '':
        c_7_4 = 0
        list5.append(c_7_4)
    else:
        list5.append(int(c_7_4))
    # (8,4)
    c_8_4 = driver.find_element(By.CSS_SELECTOR, '#c_8_4').text
    if c_8_4 == '':
        c_8_4 = 0
        list5.append(c_8_4)
    else:
        list5.append(int(c_8_4))
    list6 = []
    # (0,5)
    c_0_5 = driver.find_element(By.CSS_SELECTOR, '#c_0_5').text
    if c_0_5 == '':
        c_0_5 = 0
        list6.append(c_0_5)
    else:
        list6.append(int(c_0_5))
    # (1,5)
    c_1_5 = driver.find_element(By.CSS_SELECTOR, '#c_1_5').text
    if c_1_5 == '':
        c_1_5 = 0
        list6.append(c_1_5)
    else:
        list6.append(int(c_1_5))
    # (2,5)
    c_2_5 = driver.find_element(By.CSS_SELECTOR, '#c_2_5').text
    if c_2_5 == '':
        c_2_5 = 0
        list6.append(c_2_5)
    else:
        list6.append(int(c_2_5))
    # (3,5)
    c_3_5 = driver.find_element(By.CSS_SELECTOR, '#c_3_5').text
    if c_3_5 == '':
        c_3_5 = 0
        list6.append(c_3_5)
    else:
        list6.append(int(c_3_5))
        
    # (4,5)
    c_4_5 = driver.find_element(By.CSS_SELECTOR, '#c_4_5').text
    if c_4_5 == '':
        c_4_5 = 0
        list6.append(c_4_5)
    else:
        list6.append(int(c_4_5))
    # (5,5)
    c_5_5 = driver.find_element(By.CSS_SELECTOR, '#c_5_5').text
    if c_5_5 == '':
        c_5_5 = 0
        list6.append(c_5_5)
    else:
        list6.append(int(c_5_5))
    # (6,5)
    c_6_5 = driver.find_element(By.CSS_SELECTOR, '#c_6_5').text
    if c_6_5 == '':
        c_6_5 = 0
        list6.append(c_6_5)
    else:
        list6.append(int(c_6_5))
    # (7,5)
    c_7_5 = driver.find_element(By.CSS_SELECTOR, '#c_7_5').text
    if c_7_5 == '':
        c_7_5 = 0
        list6.append(c_7_5)
    else:
        list6.append(int(c_7_5))
    # (8,5)
    c_8_5 = driver.find_element(By.CSS_SELECTOR, '#c_8_5').text
    if c_8_5 == '':
        c_8_5 = 0
        list6.append(c_8_5)
    else:
        list6.append(int(c_8_5))
    list7 = []
    # (0,6)
    c_0_6 = driver.find_element(By.CSS_SELECTOR, '#c_0_6').text
    if c_0_6 == '':
        c_0_6 = 0
        list7.append(c_0_6)
    else:
        list7.append(int(c_0_6))
    # (1,6)
    c_1_6 = driver.find_element(By.CSS_SELECTOR, '#c_1_6').text
    if c_1_6 == '':
        c_1_6 = 0
        list7.append(c_1_6)
    else:
        list7.append(int(c_1_6))
    # (2,6)
    c_2_6 = driver.find_element(By.CSS_SELECTOR, '#c_2_6').text
    if c_2_6 == '':
        c_2_6 = 0
        list7.append(c_2_6)
    else:
        list7.append(int(c_2_6))
    # (3,6)
    c_3_6 = driver.find_element(By.CSS_SELECTOR, '#c_3_6').text
    if c_3_6 == '':
        c_3_6 = 0
        list7.append(c_3_6)
    else:
        list7.append(int(c_3_6))
    # (4,6)
    c_4_6 = driver.find_element(By.CSS_SELECTOR, '#c_4_6').text
    if c_4_6 == '':
        c_4_6 = 0
        list7.append(c_4_6)
    else:
        list7.append(int(c_4_6))
    # (5,6)
    c_5_6 = driver.find_element(By.CSS_SELECTOR, '#c_5_6').text
    if c_5_6 == '':
        c_5_6 = 0
        list7.append(c_5_6)
    else:
        list7.append(int(c_5_6))
    # (6,6)
    c_6_6 = driver.find_element(By.CSS_SELECTOR, '#c_6_6').text
    if c_6_6 == '':
        c_6_6 = 0
        list7.append(c_6_6)
    else:
        list7.append(int(c_6_6))
    # (7,6)
    c_7_6 = driver.find_element(By.CSS_SELECTOR, '#c_7_6').text
    if c_7_6 == '':
        c_7_6 = 0
        list7.append(c_7_6)
    else:
        list7.append(int(c_7_6))
    # (8,6)
    c_8_6 = driver.find_element(By.CSS_SELECTOR, '#c_8_6').text
    if c_8_6 == '':
        c_8_6 = 0
        list7.append(c_8_6)
    else:
        list7.append(int(c_8_6))
    list8 = []
    # (0,7)
    c_0_7 = driver.find_element(By.CSS_SELECTOR, '#c_0_7').text
    if c_0_7 == '':
        c_0_7 = 0
        list8.append(c_0_7)
    else:
        list8.append(int(c_0_7))
    # (1,7)
    c_1_7 = driver.find_element(By.CSS_SELECTOR, '#c_1_7').text
    if c_1_7 == '':
        c_1_7 = 0
        list8.append(c_1_7)
    else:
        list8.append(int(c_1_7))
    # (2,7)
    c_2_7 = driver.find_element(By.CSS_SELECTOR, '#c_2_7').text
    if c_2_7 == '':
        c_2_7 = 0
        list8.append(c_2_7)
    else:
        list8.append(int(c_2_7))
    # (3,7)
    c_3_7 = driver.find_element(By.CSS_SELECTOR, '#c_3_7').text
    if c_3_7 == '':
        c_3_7 = 0
        list8.append(c_3_7)
    else:
        list8.append(int(c_3_7))
    # (4,7)
    c_4_7 = driver.find_element(By.CSS_SELECTOR, '#c_4_7').text
    if c_4_7 == '':
        c_4_7 = 0
        list8.append(c_4_7)
    else:
        list8.append(int(c_4_7))
    # (5,7)
    c_5_7 = driver.find_element(By.CSS_SELECTOR, '#c_5_7').text
    if c_5_7 == '':
        c_5_7 = 0
        list8.append(c_5_7)
    else:
        list8.append(int(c_5_7))
    # (6,7)
    c_6_7 = driver.find_element(By.CSS_SELECTOR, '#c_6_7').text
    if c_6_7 == '':
        c_6_7 = 0
        list8.append(c_6_7)
    else:
        list8.append(int(c_6_7))
    # (7,7)
    c_7_7 = driver.find_element(By.CSS_SELECTOR, '#c_7_7').text
    if c_7_7 == '':
        c_7_7 = 0
        list8.append(c_7_7)
    else:
        list8.append(int(c_7_7))
    # (8,7)
    c_8_7 = driver.find_element(By.CSS_SELECTOR, '#c_8_7').text
    if c_8_7 == '':
        c_8_7 = 0
        list8.append(c_8_7)
    else:
        list8.append(int(c_8_7))
    list9 = []
    # (0,8)
    c_0_8 = driver.find_element(By.CSS_SELECTOR, '#c_0_8').text
    if c_0_8 == '':
        c_0_8 = 0
        list9.append(c_0_8)
    else:
        list9.append(int(c_0_8))
    # (1,8)
    c_1_8 = driver.find_element(By.CSS_SELECTOR, '#c_1_8').text
    if c_1_8 == '':
        c_1_8 = 0
        list9.append(c_1_8)
    else:
        list9.append(int(c_1_8))
    # (2,8)
    c_2_8 = driver.find_element(By.CSS_SELECTOR, '#c_2_8').text
    if c_2_8 == '':
        c_2_8 = 0
        list9.append(c_2_8)
    else:
        list9.append(int(c_2_8))
    # (3,8)
    c_3_8 = driver.find_element(By.CSS_SELECTOR, '#c_3_8').text
    if c_3_8 == '':
        c_3_8 = 0
        list9.append(c_3_8)
    else:
        list9.append(int(c_3_8))
    # (4,8)
    c_4_8 = driver.find_element(By.CSS_SELECTOR, '#c_4_8').text
    if c_4_8 == '':
        c_4_8 = 0
        list9.append(c_4_8)
    else:
        list9.append(int(c_4_8))
    # (5,8)
    c_5_8 = driver.find_element(By.CSS_SELECTOR, '#c_5_8').text
    if c_5_8 == '':
        c_5_8 = 0
        list9.append(c_5_8)
    else:
        list9.append(int(c_5_8))
    # (6,8)
    c_6_8 = driver.find_element(By.CSS_SELECTOR, '#c_6_8').text
    if c_6_8 == '':
        c_6_8 = 0
        list9.append(c_6_8)
    else:
        list9.append(int(c_6_8))
    # (7,8)
    c_7_8 = driver.find_element(By.CSS_SELECTOR, '#c_7_8').text
    if c_7_8 == '':
        c_7_8 = 0
        list9.append(c_7_8)
    else:
        list9.append(int(c_7_8))
    # (8,8)
    c_8_8 = driver.find_element(By.CSS_SELECTOR, '#c_8_8').text
    if c_8_8 == '':
        c_8_8 = 0
        list9.append(c_8_8)
    else:
        list9.append(int(c_8_8))
    list = []
    list.append(list1)
    list.append(list2)
    list.append(list3)
    list.append(list4)
    list.append(list5)
    list.append(list6)
    list.append(list7)
    list.append(list8)
    list.append(list9)

我们看看这里会是什么效果:

 

解数独

这里,我们就不说解数独的思路了,我们直接展示代码,代码中的m就是我们上面代码中获取的的列表值,我们只要把m传进去就可以。

    sys.setrecursionlimit(10000)
    def get_next    (m, x, y):
        for next_y in range(y + 1, 9): 
            if m[x][next_y] == 0:
                return x, next_y
        for next_x in range(x + 1, 9): 
            for next_y in range(0, 9):
                if m[next_x][next_y] == 0:
                    return next_x, next_y
        return -1, -1
    def value (m, x, y)::
        i, j = x // 3, y // 3
        grid = [m[i * 3 + r][j * 3 + c] for r in range(3) for c in range(3)]
        v = set([x for x in range(1, 10)]) - set(grid) - set(m[x]) - \
            set(list(zip(*m))[y])
        return list(v)
    def start_pos(m):
        for x in range(9):
            for y in range(9):
                if m[x][y] == 0:
                    return x, y
        return False, False
    def try_sudoku (m, x, y)::
        for v in value(m, x, y):
            m[x][y] = v
            next_x, next_y = get_next(m, x, y)
            if next_y == -1:  
                return True
            else:
                end = try_sudoku(m, next_x, next_y) 
                if end:
                    return True
                m[x][y] = 0  
    def sudoku(m):
        x, y = start_pos(m)
        try_sudoku(m, x, y)
        print(m)
        tianxie_shudu(m)
    sudoku(m)

我们这样就把数独解出来了,接下来,就是来模拟填写数独了。 

 模拟填写数独

这个思路很简单,就是判断数独中空值的地方,就填写对应的数字。

    c_0_0 = driver.find_element(By.CSS_SELECTOR, '#c_0_0').text
    if c_0_0 == '':
        print(list_m[0])
        driver.find_element(By.CSS_SELECTOR, '#c_0_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[0]}').click()
        time.sleep(1)
    else:
        pass

这段代码是通过find_element方法找到一个元素,并获取该元素的文本。然后,它检查这个文本是否为空。如果为空,它会执行以下操作:

  1. 打印一个名为list_m[0]的值。
  2. 找到一个元素,使用CSS选择器'#c_0_0'定位,并模拟点击事件。
  3. 等待1秒钟,以等待页面加载或元素切换等操作完成。
  4. 找到一个元素,使用CSS选择器'#ot{list_m[0]}'定位,并模拟点击事件。
  5. 等待1秒钟,以等待页面加载或元素切换等操作完成。

如果文本不为空,则不执行任何操作,使用pass表示跳过。

其他的都是类似的,我就不一一解释了。下面直接看代码:

    list_m = []
    for x in m:
        for y in x:
            list_m.append(y)
    print(list_m)
    driver.find_element(By.CSS_SELECTOR, '#rv').click()
    c_0_0 = driver.find_element(By.CSS_SELECTOR, '#c_0_0').text
    if c_0_0 == '':
        print(list_m[0])
        driver.find_element(By.CSS_SELECTOR, '#c_0_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[0]}').click()
        time.sleep(1)
    else:
        pass
    c_1_0 = driver.find_element(By.CSS_SELECTOR, '#c_1_0').text
    if c_1_0 == '':
        print(list_m[1])
        driver.find_element(By.CSS_SELECTOR, '#c_1_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[1]}').click()
        time.sleep(1)
    else:
        pass
    c_2_0 = driver.find_element(By.CSS_SELECTOR, '#c_2_0').text
    if c_2_0 == '':
        print(list_m[2])
        driver.find_element(By.CSS_SELECTOR, '#c_2_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[2]}').click()
        time.sleep(1)
    else:
        pass
    c_3_0 = driver.find_element(By.CSS_SELECTOR, '#c_3_0').text
    if c_3_0 == '':
        print(list_m[3])
        driver.find_element(By.CSS_SELECTOR, '#c_3_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[3]}').click()
        time.sleep(1)
    else:
        pass
    c_4_0 = driver.find_element(By.CSS_SELECTOR, '#c_4_0').text
    if c_4_0 == '':
        print(list_m[4])
        driver.find_element(By.CSS_SELECTOR, '#c_4_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[4]}').click()
        time.sleep(1)
    else:
        pass
    c_5_0 = driver.find_element(By.CSS_SELECTOR, '#c_5_0').text
    if c_5_0 == '':
        print(list_m[5])
        driver.find_element(By.CSS_SELECTOR, '#c_5_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[5]}').click()
        time.sleep(1)
    else:
        pass
    c_6_0 = driver.find_element(By.CSS_SELECTOR, '#c_6_0').text
    if c_6_0 == '':
        print(list_m[6])
        driver.find_element(By.CSS_SELECTOR, '#c_6_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[6]}').click()
        time.sleep(1)
    else:
        pass
    c_7_0 = driver.find_element(By.CSS_SELECTOR, '#c_7_0').text
    if c_7_0 == '':
        print(list_m[7])
        driver.find_element(By.CSS_SELECTOR, '#c_7_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[7]}').click()
        time.sleep(1)
    else:
        pass
    c_8_0 = driver.find_element(By.CSS_SELECTOR, '#c_8_0').text
    if c_8_0 == '':
        print(list_m[8])
        driver.find_element(By.CSS_SELECTOR, '#c_8_0').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[8]}').click()
        time.sleep(1)
    else:
        pass
    c_0_1 = driver.find_element(By.CSS_SELECTOR, '#c_0_1').text
    if c_0_1 == '':
        print(list_m[9])
        driver.find_element(By.CSS_SELECTOR, '#c_0_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[9]}').click()
        time.sleep(1)
    else:
        pass
    c_1_1 = driver.find_element(By.CSS_SELECTOR, '#c_1_1').text
    if c_1_1 == '':
        print(list_m[10])
        driver.find_element(By.CSS_SELECTOR, '#c_1_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[10]}').click()
        time.sleep(1)
    else:
        pass
    c_2_1 = driver.find_element(By.CSS_SELECTOR, '#c_2_1').text
    if c_2_1 == '':
        print(list_m[11])
        driver.find_element(By.CSS_SELECTOR, '#c_2_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[11]}').click()
        time.sleep(1)
    else:
        pass
    c_3_1 = driver.find_element(By.CSS_SELECTOR, '#c_3_1').text
    if c_3_1 == '':
        print(list_m[12])
        driver.find_element(By.CSS_SELECTOR, '#c_3_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[12]}').click()
        time.sleep(1)
    else:
        pass
    c_4_1 = driver.find_element(By.CSS_SELECTOR, '#c_4_1').text
    if c_4_1 == '':
        print(list_m[13])
        driver.find_element(By.CSS_SELECTOR, '#c_4_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[13]}').click()
        time.sleep(1)
    else:
        pass
    c_5_1 = driver.find_element(By.CSS_SELECTOR, '#c_5_1').text
    if c_5_1 == '':
        print(list_m[14])
        driver.find_element(By.CSS_SELECTOR, '#c_5_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[14]}').click()
        time.sleep(1)
    else:
        pass
    c_6_1 = driver.find_element(By.CSS_SELECTOR, '#c_6_1').text
    if c_6_1 == '':
        print(list_m[15])
        driver.find_element(By.CSS_SELECTOR, '#c_6_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[15]}').click()
        time.sleep(1)
    else:
        pass
    c_7_1 = driver.find_element(By.CSS_SELECTOR, '#c_7_1').text
    if c_7_1 == '':
        print(list_m[16])
        driver.find_element(By.CSS_SELECTOR, '#c_7_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[16]}').click()
        time.sleep(1)
    else:
        pass
    c_8_1 = driver.find_element(By.CSS_SELECTOR, '#c_8_1').text
    if c_8_1 == '':
        print(list_m[17])
        driver.find_element(By.CSS_SELECTOR, '#c_8_1').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[17]}').click()
        time.sleep(1)
    else:
        pass
    c_0_2 = driver.find_element(By.CSS_SELECTOR, '#c_0_2').text
    if c_0_2 == '':
        print(list_m[18])
        driver.find_element(By.CSS_SELECTOR, '#c_0_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[18]}').click()
        time.sleep(1)
    else:
        pass
    c_1_2 = driver.find_element(By.CSS_SELECTOR, '#c_1_2').text
    if c_1_2 == '':
        print(list_m[19])
        driver.find_element(By.CSS_SELECTOR, '#c_1_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[19]}').click()
        time.sleep(1)
    else:
        pass
    c_2_2 = driver.find_element(By.CSS_SELECTOR, '#c_2_2').text
    if c_2_2 == '':
        print(list_m[20])
        driver.find_element(By.CSS_SELECTOR, '#c_2_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[20]}').click()
        time.sleep(1)
    else:
        pass
    c_3_2 = driver.find_element(By.CSS_SELECTOR, '#c_3_2').text
    if c_3_2 == '':
        print(list_m[21])
        driver.find_element(By.CSS_SELECTOR, '#c_3_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[21]}').click()
        time.sleep(1)
    else:
        pass
    c_4_2 = driver.find_element(By.CSS_SELECTOR, '#c_4_2').text
    if c_4_2 == '':
        print(list_m[22])
        driver.find_element(By.CSS_SELECTOR, '#c_4_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[22]}').click()
        time.sleep(1)
    else:
        pass
    c_5_2 = driver.find_element(By.CSS_SELECTOR, '#c_5_2').text
    if c_5_2 == '':
        print(list_m[23])
        driver.find_element(By.CSS_SELECTOR, '#c_5_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[23]}').click()
        time.sleep(1)
    else:
        pass
    c_6_2 = driver.find_element(By.CSS_SELECTOR, '#c_6_2').text
    if c_6_2 == '':
        print(list_m[24])
        driver.find_element(By.CSS_SELECTOR, '#c_6_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[24]}').click()
        time.sleep(1)
    else:
        pass
    c_7_2 = driver.find_element(By.CSS_SELECTOR, '#c_7_2').text
    if c_7_2 == '':
        print(list_m[25])
        driver.find_element(By.CSS_SELECTOR, '#c_7_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[25]}').click()
        time.sleep(1)
    else:
        pass
    c_8_2 = driver.find_element(By.CSS_SELECTOR, '#c_8_2').text
    if c_8_2 == '':
        print(list_m[26])
        driver.find_element(By.CSS_SELECTOR, '#c_8_2').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[26]}').click()
        time.sleep(1)
    else:
        pass
    c_0_3 = driver.find_element(By.CSS_SELECTOR, '#c_0_3').text
    if c_0_3 == '':
        print(list_m[27])
        driver.find_element(By.CSS_SELECTOR, '#c_0_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[27]}').click()
        time.sleep(1)
    else:
        pass
    c_1_3 = driver.find_element(By.CSS_SELECTOR, '#c_1_3').text
    if c_1_3 == '':
        print(list_m[28])
        driver.find_element(By.CSS_SELECTOR, '#c_1_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[28]}').click()
        time.sleep(1)
    else:
        pass
    c_2_3 = driver.find_element(By.CSS_SELECTOR, '#c_2_3').text
    if c_2_3 == '':
        print(list_m[29])
        driver.find_element(By.CSS_SELECTOR, '#c_2_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[29]}').click()
        time.sleep(1)
    else:
        pass
    c_3_3 = driver.find_element(By.CSS_SELECTOR, '#c_3_3').text
    if c_3_3 == '':
        print(list_m[30])
        driver.find_element(By.CSS_SELECTOR, '#c_3_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[30]}').click()
        time.sleep(1)
    else:
        pass
    c_4_3 = driver.find_element(By.CSS_SELECTOR, '#c_4_3').text
    if c_4_3 == '':
        print(list_m[31])
        driver.find_element(By.CSS_SELECTOR, '#c_4_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[31]}').click()
        time.sleep(1)
    else:
        pass
    c_5_3 = driver.find_element(By.CSS_SELECTOR, '#c_5_3').text
    if c_5_3 == '':
        print(list_m[32])
        driver.find_element(By.CSS_SELECTOR, '#c_5_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[32]}').click()
        time.sleep(1)
    else:
        pass
    c_6_3 = driver.find_element(By.CSS_SELECTOR, '#c_6_3').text
    if c_6_3 == '':
        print(list_m[33])
        driver.find_element(By.CSS_SELECTOR, '#c_6_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[33]}').click()
        time.sleep(1)
    else:
        pass
    c_7_3 = driver.find_element(By.CSS_SELECTOR, '#c_7_3').text
    if c_7_3 == '':
        print(list_m[34])
        driver.find_element(By.CSS_SELECTOR, '#c_7_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[34]}').click()
        time.sleep(1)
    else:
        pass
    c_8_3 = driver.find_element(By.CSS_SELECTOR, '#c_8_3').text
    if c_8_3 == '':
        print(list_m[35])
        driver.find_element(By.CSS_SELECTOR, '#c_8_3').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[35]}').click()
        time.sleep(1)
    else:
        pass
    c_0_4 = driver.find_element(By.CSS_SELECTOR, '#c_0_4').text
    if c_0_4 == '':
        print(list_m[36])
        driver.find_element(By.CSS_SELECTOR, '#c_0_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[36]}').click()
        time.sleep(1)
    else:
        pass
    c_1_4 = driver.find_element(By.CSS_SELECTOR, '#c_1_4').text
    if c_1_4 == '':
        print(list_m[37])
        driver.find_element(By.CSS_SELECTOR, '#c_1_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[37]}').click()
        time.sleep(1)
    else:
        pass
    c_2_4 = driver.find_element(By.CSS_SELECTOR, '#c_2_4').text
    if c_2_4 == '':
        print(list_m[38])
        driver.find_element(By.CSS_SELECTOR, '#c_2_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[38]}').click()
        time.sleep(1)
    else:
        pass
    c_3_4 = driver.find_element(By.CSS_SELECTOR, '#c_3_4').text
    if c_3_4 == '':
        print(list_m[39])
        driver.find_element(By.CSS_SELECTOR, '#c_3_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[39]}').click()
        time.sleep(1)
    else:
        pass
    c_4_4 = driver.find_element(By.CSS_SELECTOR, '#c_4_4').text
    if c_4_4 == '':
        print(list_m[40])
        driver.find_element(By.CSS_SELECTOR, '#c_4_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[40]}').click()
        time.sleep(1)
    else:
        pass
    c_5_4 = driver.find_element(By.CSS_SELECTOR, '#c_5_4').text
    if c_5_4 == '':
        print(list_m[41])
        driver.find_element(By.CSS_SELECTOR, '#c_5_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[41]}').click()
        time.sleep(1)
    else:
        pass
    c_6_4 = driver.find_element(By.CSS_SELECTOR, '#c_6_4').text
    if c_6_4 == '':
        print(list_m[42])
        driver.find_element(By.CSS_SELECTOR, '#c_6_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[42]}').click()
        time.sleep(1)
    else:
        pass
    c_7_4 = driver.find_element(By.CSS_SELECTOR, '#c_7_4').text
    if c_7_4 == '':
        print(list_m[43])
        driver.find_element(By.CSS_SELECTOR, '#c_7_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[43]}').click()
        time.sleep(1)
    else:
        pass
    c_8_4 = driver.find_element(By.CSS_SELECTOR, '#c_8_4').text
    if c_8_4 == '':
        print(list_m[44])
        driver.find_element(By.CSS_SELECTOR, '#c_8_4').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[44]}').click()
        time.sleep(1)
    else:
        pass
    c_0_5 = driver.find_element(By.CSS_SELECTOR, '#c_0_5').text
    if c_0_5 == '':
        print(list_m[45])
        driver.find_element(By.CSS_SELECTOR, '#c_0_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[45]}').click()
        time.sleep(1)
    else:
        pass
    c_1_5 = driver.find_element(By.CSS_SELECTOR, '#c_1_5').text
    if c_1_5 == '':
        print(list_m[46])
        driver.find_element(By.CSS_SELECTOR, '#c_1_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[46]}').click()
        time.sleep(1)
    else:
        pass
    c_2_5 = driver.find_element(By.CSS_SELECTOR, '#c_2_5').text
    if c_2_5 == '':
        print(list_m[47])
        driver.find_element(By.CSS_SELECTOR, '#c_2_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[47]}').click()
        time.sleep(1)
    else:
        pass
    c_3_5 = driver.find_element(By.CSS_SELECTOR, '#c_3_5').text
    if c_3_5 == '':
        print(list_m[48])
        driver.find_element(By.CSS_SELECTOR, '#c_3_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[48]}').click()
        time.sleep(1)
    else:
        pass
    c_4_5 = driver.find_element(By.CSS_SELECTOR, '#c_4_5').text
    if c_4_5 == '':
        print(list_m[49])
        driver.find_element(By.CSS_SELECTOR, '#c_4_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[49]}').click()
        time.sleep(1)
    else:
        pass
    c_5_5 = driver.find_element(By.CSS_SELECTOR, '#c_5_5').text
    if c_5_5 == '':
        print(list_m[50])
        driver.find_element(By.CSS_SELECTOR, '#c_5_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[50]}').click()
        time.sleep(1)
    else:
        pass
    c_6_5 = driver.find_element(By.CSS_SELECTOR, '#c_6_5').text
    if c_6_5 == '':
        print(list_m[51])
        driver.find_element(By.CSS_SELECTOR, '#c_6_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[51]}').click()
        time.sleep(1)
    else:
        pass
    c_7_5 = driver.find_element(By.CSS_SELECTOR, '#c_7_5').text
    if c_7_5 == '':
        print(list_m[52])
        driver.find_element(By.CSS_SELECTOR, '#c_7_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[52]}').click()
        time.sleep(1)
    else:
        pass
    c_8_5 = driver.find_element(By.CSS_SELECTOR, '#c_8_5').text
    if c_8_5 == '':
        print(list_m[53])
        driver.find_element(By.CSS_SELECTOR, '#c_8_5').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[53]}').click()
        time.sleep(1)
    else:
        pass
    c_0_6 = driver.find_element(By.CSS_SELECTOR, '#c_0_6').text
    if c_0_6 == '':
        print(list_m[54])
        driver.find_element(By.CSS_SELECTOR, '#c_0_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[54]}').click()
        time.sleep(1)
    else:
        pass
    c_1_6 = driver.find_element(By.CSS_SELECTOR, '#c_1_6').text
    if c_1_6 == '':
        print(list_m[55])
        driver.find_element(By.CSS_SELECTOR, '#c_1_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[55]}').click()
        time.sleep(1)
    else:
        pass
    c_2_6 = driver.find_element(By.CSS_SELECTOR, '#c_2_6').text
    if c_2_6 == '':
        print(list_m[56])
        driver.find_element(By.CSS_SELECTOR, '#c_2_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[56]}').click()
        time.sleep(1)
    else:
        pass
    c_3_6 = driver.find_element(By.CSS_SELECTOR, '#c_3_6').text
    if c_3_6 == '':
        print(list_m[57])
        driver.find_element(By.CSS_SELECTOR, '#c_3_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[57]}').click()
        time.sleep(1)
    else:
        pass
    c_4_6 = driver.find_element(By.CSS_SELECTOR, '#c_4_6').text
    if c_4_6 == '':
        print(list_m[58])
        driver.find_element(By.CSS_SELECTOR, '#c_4_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[58]}').click()
        time.sleep(1)
    else:
        pass
    c_5_6 = driver.find_element(By.CSS_SELECTOR, '#c_5_6').text
    if c_5_6 == '':
        print(list_m[59])
        driver.find_element(By.CSS_SELECTOR, '#c_5_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[59]}').click()
        time.sleep(1)
    else:
        pass
    c_6_6 = driver.find_element(By.CSS_SELECTOR, '#c_6_6').text
    if c_6_6 == '':
        print(list_m[60])
        driver.find_element(By.CSS_SELECTOR, '#c_6_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[60]}').click()
        time.sleep(1)
    else:
        pass
    c_7_6 = driver.find_element(By.CSS_SELECTOR, '#c_7_6').text
    if c_7_6 == '':
        print(list_m[61])
        driver.find_element(By.CSS_SELECTOR, '#c_7_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[61]}').click()
        time.sleep(1)
    else:
        pass
    c_8_6 = driver.find_element(By.CSS_SELECTOR, '#c_8_6').text
    if c_8_6 == '':
        print(list_m[62])
        driver.find_element(By.CSS_SELECTOR, '#c_8_6').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[62]}').click()
        time.sleep(1)
    else:
        pass
    c_0_7 = driver.find_element(By.CSS_SELECTOR, '#c_0_7').text
    if c_0_7 == '':
        print(list_m[63])
        driver.find_element(By.CSS_SELECTOR, '#c_0_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[63]}').click()
        time.sleep(1)
    else:
        pass
    c_1_7 = driver.find_element(By.CSS_SELECTOR, '#c_1_7').text
    if c_1_7 == '':
        print(list_m[64])
        driver.find_element(By.CSS_SELECTOR, '#c_1_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[64]}').click()
        time.sleep(1)
    else:
        pass
    c_2_7 = driver.find_element(By.CSS_SELECTOR, '#c_2_7').text
    if c_2_7 == '':
        print(list_m[65])
        driver.find_element(By.CSS_SELECTOR, '#c_2_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[65]}').click()
        time.sleep(1)
    else:
        pass
    c_3_7 = driver.find_element(By.CSS_SELECTOR, '#c_3_7').text
    if c_3_7 == '':
        print(list_m[66])
        driver.find_element(By.CSS_SELECTOR, '#c_3_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[66]}').click()
        time.sleep(1)
    else:
        pass
    c_4_7 = driver.find_element(By.CSS_SELECTOR, '#c_4_7').text
    if c_4_7 == '':
        print(list_m[67])
        driver.find_element(By.CSS_SELECTOR, '#c_4_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[67]}').click()
        time.sleep(1)
    else:
        pass
    c_5_7 = driver.find_element(By.CSS_SELECTOR, '#c_5_7').text
    if c_5_7 == '':
        print(list_m[68])
        driver.find_element(By.CSS_SELECTOR, '#c_5_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[68]}').click()
        time.sleep(1)
    else:
        pass
    c_6_7 = driver.find_element(By.CSS_SELECTOR, '#c_6_7').text
    if c_6_7 == '':
        print(list_m[69])
        driver.find_element(By.CSS_SELECTOR, '#c_6_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[69]}').click()
        time.sleep(1)
    else:
        pass
    c_7_7 = driver.find_element(By.CSS_SELECTOR, '#c_7_7').text
    if c_7_7 == '':
        print(list_m[70])
        driver.find_element(By.CSS_SELECTOR, '#c_7_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[70]}').click()
        time.sleep(1)
    else:
        pass
    c_8_7 = driver.find_element(By.CSS_SELECTOR, '#c_8_7').text
    if c_8_7 == '':
        print(list_m[71])
        driver.find_element(By.CSS_SELECTOR, '#c_8_7').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[71]}').click()
        time.sleep(1)
    else:
        pass
    c_0_8 = driver.find_element(By.CSS_SELECTOR, '#c_0_8').text
    if c_0_8 == '':
        print(list_m[72])
        driver.find_element(By.CSS_SELECTOR, '#c_0_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[72]}').click()
        time.sleep(1)
    else:
        pass
    c_1_8 = driver.find_element(By.CSS_SELECTOR, '#c_1_8').text
    if c_1_8 == '':
        print(list_m[73])
        driver.find_element(By.CSS_SELECTOR, '#c_1_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[73]}').click()
        time.sleep(1)
    else:
        pass
    c_2_8 = driver.find_element(By.CSS_SELECTOR, '#c_2_8').text
    if c_2_8 == '':
        print(list_m[74])
        driver.find_element(By.CSS_SELECTOR, '#c_2_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[74]}').click()
        time.sleep(1)
    else:
        pass
    c_3_8 = driver.find_element(By.CSS_SELECTOR, '#c_3_8').text
    if c_3_8 == '':
        print(list_m[75])
        driver.find_element(By.CSS_SELECTOR, '#c_3_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[75]}').click()
        time.sleep(1)
    else:
        pass
    c_4_8 = driver.find_element(By.CSS_SELECTOR, '#c_4_8').text
    if c_4_8 == '':
        print(list_m[76])
        driver.find_element(By.CSS_SELECTOR, '#c_4_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[76]}').click()
        time.sleep(1)
    else:
        pass
    c_5_8 = driver.find_element(By.CSS_SELECTOR, '#c_5_8').text
    if c_5_8 == '':
        print(list_m[77])
        driver.find_element(By.CSS_SELECTOR, '#c_5_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[77]}').click()
        time.sleep(1)
    else:
        pass
    c_6_8 = driver.find_element(By.CSS_SELECTOR, '#c_6_8').text
    if c_6_8 == '':
        print(list_m[78])
        driver.find_element(By.CSS_SELECTOR, '#c_6_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[78]}').click()
        time.sleep(1)
    else:
        pass
    c_7_8 = driver.find_element(By.CSS_SELECTOR, '#c_7_8').text
    if c_7_8 == '':
        print(list_m[79])
        driver.find_element(By.CSS_SELECTOR, '#c_7_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[79]}').click()
        time.sleep(1)
    else:
        pass
    c_8_8 = driver.find_element(By.CSS_SELECTOR, '#c_8_8').text
    if c_8_8 == '':
        print(list_m[80])
        driver.find_element(By.CSS_SELECTOR, '#c_8_8').click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR, f'#ot{list_m[80]}').click()
        time.sleep(1)
    else:
        pass

我们看看最后的效果吧:

总结

由于篇幅有限,我这个源码会放在评论区或者放个资源,感兴趣的可以自行下载,这个功能其实特别简单,就是简单的selenium的操作,有什么疑问可以在评论区说说。

6adf31c8c5dd4e6a83314f4805b30bc1.jpg

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/735161.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

一、枚举类型——新特性(模式匹配-覆盖范围)

模式匹配会引导你逐渐使用 sealed 关键字,这有助于确保你已覆盖了所有可能传入选择器表达式的类型。不过接下来再看一个示例: SealedPatternMatch.java JDK 17 import java.util.List;sealed interface Transport { };record Bicycle(String id) impleme…

第八章:YOLO v3(渐进式改进)网络详解

(目标检测篇)系列文章目录 第一章:R-CNN网络详解 第二章:Fast R-CNN网络详解 第三章:Faster R-CNN网络详解 第四章:SSD网络详解 第五章:Mask R-CNN网络详解 第六章:YOLO v1网络详解 第七章:YOLO v2网络详解 第八章:YOLO v3网络详解 文章目录 系列文章目录技…

ruiyo-cloud-plus集成shardingsphere-proxy进行分库分表

一、什么shardingsphere-proxy Sharding-Proxy是ShardingSphere的第二个产品。 它定位为透明化的数据库代理端,提供封装了数据库二进制协议的服务端版本,用于完成对异构语言的支持。 目前先提供MySQL版本,它可以使用任何兼容MySQL协议的访问…

阿里云轻量应用服务器和云服务器ECS有什么区别?

阿里云服务器ECS和轻量应用服务器有什么区别?云服务器ECS是明星级云服务器,轻量应用服务器可以理解为简化版的云服务器ECS,轻量适用于单机应用,云服务器ECS适用于集群类高可用高容灾应用,阿里云百科来详细说下阿里云轻…

用栈实现队列——力扣232

题目描述 思路 class MyQueue {stack<int> inStack, outStack;void in2out(){while(!inStack.empty()){outStack.push(inStack.top());inStack.pop();}} public:MyQueue() {}void push(int x) {inStack.push(x);}int pop() {if(outStack.empty()){in2out();}int x out…

etiger.vip 答案 1907最高的山

1907.最高的山 题目描述 有n座山&#xff0c;编号从1到n&#xff0c;第i座山的高度为hi&#xff0c;请问其中最高的山是哪一座&#xff1f; 输入输出格式 输入格式 第一行一个正整数n&#xff0c;表示山的数量&#xff0c;n<1000。 第二行n个正整数&#xff0c;中间由空…

四元数转换为一个旋转矩阵

#include <iostream> #include <vector> #include <Eigen/Core> #include <Eigen/Geometry> #

回调函数的应用(sqort函数)——指针进阶(三)

目录 前言 什么是回调函数&#xff1f; 回调函数的应用 qsort函数 qsort函数的使用 qsort模拟实现 模拟函数的使用 总结 前言 回调函数是一种非常常见的编程技术&#xff0c;在许多不同的编程语言和框架中都有广泛的应用。但它到底是什么&#xff0c;以及如何使用呢&#xff1…

-XX:MaxDirectMemorySize

-XX:MaxDirectMemorySize最大堆外内存大小&#xff0c;此参数的含义是当Direct ByteBuffer分配的堆外内存到达指定大小后就触发Full GC。首先可以在jdk文档中找到&#xff1a;关于MaxDirectMemorySize内存的描述&#xff1a;Sets the maximum total size (in bytes) of the New…

02_06内核cpu信息及内存布局及堆管理及内存使用情况

linux_cpu_大致信息查看 因为需要知道当前linux cpu是多少位的,才能知道内核空间的分布 64位Linux-般使用48位来表示虚拟地址空间&#xff0c;45位表示物理地址。通过命令: cat/proc/cpuinfo。 查看Linux内核位数和proc文件系统输出系统软硬件信息如下: 通过cat /proc/cpuinfo…

RabbitMQ笔记--消息中间件,rabbitmq安装及简单使用

1.消息中间件 消息&#xff1a;指在应用间传送的数据。 消息队列中间件&#xff1a;指利用高效可靠的消息传递机制进行与平台无关的数据交流&#xff0c;并基于数据通信来进行分布式系统的集成。通过提供消息传递和消息排队模型&#xff0c;可以在分布式环境下扩展进程间的通…

unittest自动化测试之unittest封装方法

目录 封装方法 完整代码 总结&#xff1a; 封装方法 在编写自动化脚本的时候&#xff0c;都要求代码简介&#xff0c;上一篇unittest---unittest断言中代码重复性比较多&#xff0c;我们进行一次简单的优化&#xff08;优化方法不同&#xff0c;请勿喷&#xff09; 我们可以…

Video4linux: cannot set V4L control... 解决方案

问题描述 最近在用USB_CAM读取摄像头的时候&#xff0c;总是出现如下问题&#xff1a; 思来想去真是难受。 问题分析 在usb_cam/ config /usb_cam.yaml文件中&#xff0c;把他报错的东西加在ignore里面即可。 首先打开camera_driver.cpp文件&#xff0c;把908行的printf语句…

ARM64学习笔记---建立异常向量表(二)

源码: #include "mm.h" #include "sysregs.h".section .rodata .align 3 .globl el_string1 el_string1:.string "Booting at EL".section ".text.boot" .globl _start _start://读取mpidr_el1寄存器的值&#xff0c;该寄存器决定了…

学无止境·MySQL(4-3)(多表查询加强版-------更新版)

比4-2表中多几个数据&#xff0c;更具备普遍性 试题2&#xff08;更新加强版&#xff09;1、创建表2、找出销售部门中年纪最大的员工的姓名3、求财务部门最低工资的员工姓名4、列出每个部门收入总和高于9000的部门名称5、求工资在7500到8500元之间&#xff0c;年龄最大的人的姓…

MySql 高级-0706

1. MySQL 架构 1.1 MySQL 简介 mysql内核 sql优化攻城狮 mysql服务器的优化 各种参数常量设定 查询语句优化 主从复制 软硬件升级 容灾备份 sql编程 1.2 MySQL逻辑架构介绍 1.2.1第1层:连接层 ** 系统(客户端)访问 MySQL 服务器前&#xff0c;做的第一件事就是建立 TCP 连接…

HTML <map> 标签

实例 带有可点击区域的图像映射: <img src="planets.jpg" border="0" usemap="#planetmap" alt="Planets" /><map name="planetmap" id="planetmap"><area shape="circle" coords=&q…

多元分类预测 | Matlab 粒子群算法(PSO)优化xgboost的分类预测,多输入单输出模型。PSO-xgboost分类预测模型

文章目录 效果一览文章概述部分源码参考资料效果一览 文章概述 多元分类预测 | Matlab 粒子群算法(PSO)优化xgboost的分类预测,多输入单输出模型。PSO-xgboost分类预测模型 多特征输入单输出的二分类及多分类模型。程序内注释详细,直接替换数据就可以用。程序语言为matlab,程…

利用Python构建科学的交易系统:以趋势为例

在股票市场中&#xff0c;趋势被认为是一个非常重要的概念。趋势可以通过股票的价格、交易量等指标来确定。如果一只股票的价格在持续上涨&#xff0c;那么我们可以认为该股票处于上升趋势&#xff1b;反之&#xff0c;如果一只股票的价格在持续下跌&#xff0c;那么我们认为该…

物理机到容器的发展历程

物理机到容器的发展历程 物理主机虚拟化技术什么是虚拟化&#xff1f;虚拟化分类主流的虚拟化技术KVMLXC 容器Docker与Containerd容器编排为什么要编排工具容器编排工具有哪些&#xff1f; 物理主机 从传统的物理服务到现如今的云服务的发展离不开应用服务的拆分&#xff0c;由…