Files
NYT_Sudoku/NYT.py
2023-11-15 10:52:28 -05:00

49 lines
1.7 KiB
Python

import requests
from bs4 import BeautifulSoup
import json
import numpy as np
import time
from pprint import pprint
url = "https://www.nytimes.com/puzzles/sudoku/hard"
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["hard"]["puzzle_data"]["puzzle"] ).reshape(9,9).tolist()
hard_puzzle_id = Dict["hard"]["puzzle_id"]
hard_published = Dict["hard"]["published"]
hard_print_date = Dict["hard"]["print_date"]
hard = "".join(str(x) for x in Dict["hard"]["puzzle_data"]["puzzle"])
med = "".join(str(x) for x in Dict["medium"]["puzzle_data"]["puzzle"])
easy = "".join(str(x) for x in Dict["easy"]["puzzle_data"]["puzzle"])
# Import writer class from csv module
from csv import writer
# List that we want to add as a new row
HardList = ["Hard",Dict["hard"]["puzzle_id"],Dict["hard"]["published"],Dict["hard"]["print_date"],hard]
MedList = ["Medium",Dict["medium"]["puzzle_id"],Dict["medium"]["published"],Dict["medium"]["print_date"],hard]
EasyList = ["Easy",Dict["easy"]["puzzle_id"],Dict["easy"]["published"],Dict["easy"]["print_date"],hard]
# Open our existing CSV file in append mode
# Create a file object for this file
with open('NYTSudoku.csv', 'a') as f_object:
# Pass this file object to csv.writer()
# and get a writer object
writer_object = writer(f_object)
# Pass the list as an argument into
# the writerow()
writer_object.writerow(HardList)
writer_object.writerow(MedList)
writer_object.writerow(EasyList)
# Close the file object
f_object.close()