fixing script

This commit is contained in:
2023-11-15 20:13:32 +00:00
parent 621be6e8f0
commit e26954fd79

33
NYT.py
View File

@@ -4,33 +4,25 @@ import json
import numpy as np import numpy as np
import time import time
from pprint import pprint from pprint import pprint
from csv import writer
def get_puzzle(difficulty):
url = "https://www.nytimes.com/puzzles/sudoku/hard" url = "https://www.nytimes.com/puzzles/sudoku/hard"
page = requests.get(url) page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser") soup = BeautifulSoup(page.content, "html.parser")
table = soup.find_all("div", {"class":"pz-game-screen"})[0] table = soup.find_all("div", {"class":"pz-game-screen"})[0]
script = table.find_all("script", {"type":"text/javascript"}) script = table.find_all("script", {"type":"text/javascript"})
text = script[0].get_text().split("=")[1] text = script[0].get_text().split("=")[1]
Dict = json.loads(text) Dict = json.loads(text)
grid = np.array( Dict["hard"]["puzzle_data"]["puzzle"] ).reshape(9,9).tolist() grid = np.array( Dict[difficulty]["puzzle_data"]["puzzle"] ).reshape(9,9).tolist()
hard_puzzle_id = Dict["hard"]["puzzle_id"] puzzle_id = Dict[difficulty]["puzzle_id"]
hard_published = Dict["hard"]["published"] published = Dict[difficulty]["published"]
hard_print_date = Dict["hard"]["print_date"] print_date = Dict[difficulty]["print_date"]
hard = "".join(str(x) for x in Dict["hard"]["puzzle_data"]["puzzle"]) puzzle_data = "".join(str(x) for x in Dict[difficulty]["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 # List that we want to add as a new row
HardList = ["Hard",Dict["hard"]["puzzle_id"],Dict["hard"]["published"],Dict["hard"]["print_date"],hard] csvrow = [difficulty,puzzle_id,published,print_date,puzzle_data]
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 # Open our existing CSV file in append mode
# Create a file object for this file # Create a file object for this file
@@ -40,9 +32,12 @@ with open('NYTSudoku.csv', 'a') as f_object:
writer_object = writer(f_object) writer_object = writer(f_object)
# Pass the list as an argument into # Pass the list as an argument into
# the writerow() # the writerow()
writer_object.writerow(HardList) writer_object.writerow(csvrow)
writer_object.writerow(MedList)
writer_object.writerow(EasyList)
# Close the file object # Close the file object
f_object.close() f_object.close()
if __name__ == '__main__':
get_puzzle("hard")
get_puzzle("medium")
get_puzzle("easy")