-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebscrap_sudoku_NY.py
49 lines (41 loc) · 1.93 KB
/
webscrap_sudoku_NY.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import webbrowser
import requests
from bs4 import BeautifulSoup
import json
import numpy as np
from Backtrack import backtrack, backtrack_visualiser
from greedyBacktrack import greedyBacktrack, greedyBacktrack_visualiser
from zigzagBacktrack import zigzagBacktrack, zigzagBacktrack_visualiser
from reverseBacktrack import reverseBacktrack, reverseBacktrack_visualiser
from utilities import ConvertToChar, EnterBoard, printBoard
import time
from sys import platform
import os
import pyautogui as pg
difficulty = "hard"
url = "https://www.nytimes.com/puzzles/sudoku/" + difficulty
if platform == "linux" or platform == "linux2":
chrome_path = '/usr/bin/google-chrome %s --incognito'
webbrowser.get(chrome_path).open(url)
elif platform == "darwin":
chrome_path = 'open -a /Applications/Google\ Chrome.app %s --incognito'
os.system("open -na \"Google Chrome\" --args --incognito \"{}\"".format(url))
elif platform == "win32":
chrome_path = 'C:/Program Files/Google/Chrome/Application/chrome.exe --incognito %s'
webbrowser.get(chrome_path).open_new(url)
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
table = soup.find_all("div", {"class":"pz-game-screen"})[0]
script = table.find_all("script", {"type":"text/javascript"})
text = script[0].get_text().split("=")[1]
Dict = json.loads(text)
grid = np.array( Dict[difficulty]["puzzle_data"]["puzzle"] ).reshape(9,9).tolist()
def direct_enter(grid):
greedyBacktrack(grid) # solve the puzzle using greedy backtrack
grid = ConvertToChar(grid) # convert the grid to characters
printBoard(grid) # print the grid
EnterBoard(grid) # enter the grid into the website
time.sleep(3) # wait for 3 seconds
pg.press('left', presses = 9) # press left nine times to ensure that the first cell is selected
greedyBacktrack_visualiser(grid) # visualise the backtrack
#direct_enter(grid) # enter the grid into the website