Files
NYT_Sudoku/NYT.py
2023-11-15 20:15:11 +00:00

43 lines
1.5 KiB
Python

import requests
from bs4 import BeautifulSoup
import json
import numpy as np
import time
from pprint import pprint
from csv import writer
def get_puzzle(difficulty):
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[difficulty]["puzzle_data"]["puzzle"] ).reshape(9,9).tolist()
puzzle_id = Dict[difficulty]["puzzle_id"]
published = Dict[difficulty]["published"]
print_date = Dict[difficulty]["print_date"]
puzzle_data = "".join(str(x) for x in Dict[difficulty]["puzzle_data"]["puzzle"])
# List that we want to add as a new row
csvrow = [difficulty,puzzle_id,published,print_date,puzzle_data]
# 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(csvrow)
# Close the file object
f_object.close()
if __name__ == '__main__':
get_puzzle("hard")
get_puzzle("medium")
get_puzzle("easy")