Create NYT.py

This commit is contained in:
2023-11-15 10:47:03 -05:00
committed by GitHub
parent 5c5177103c
commit 62d96dd65f

48
NYT.py Normal file
View File

@@ -0,0 +1,48 @@
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/" + difficulty
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()
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()