Question:
- I am trying to create a csv file every hour that contains the headlines from CNN
- I am not able to deploy it. The deployment fails before there are any logs
- I am a total beginner, so I am sure I am missing something very basic
Repl link:
https://replit.com/@makanamini/Project-Swift?v=1
import os
import requests
from bs4 import BeautifulSoup
import csv
from datetime import datetime
import time
url = "https://www.cnn.com/"
def scrape_headlines():
now = datetime.now()
# Format date and time with leading zeros
date = "{:02d}-{:02d}-{:02d}".format(now.year, now.month, now.day)
time = "{:02d}{:02d}".format(now.hour, now.minute)
folder_name = "cnn"
if not os.path.exists(folder_name):
os.makedirs(folder_name)
filename = os.path.join(folder_name, "cnn.com-{}-{}.csv".format(date, time))
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
headlines = []
for headline in soup.find_all("span", {"data-editable": "headline"}):
headlines.append(headline.text.strip())
with open(filename, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Headline", "Rank"])
rank = 1
for headline in headlines:
writer.writerow([headline, rank])
rank += 1
# Add code to run at the top of the hour
while True:
current_time = datetime.now().strftime("%M")
if current_time == "00":
scrape_headlines()
time.sleep(60) # Sleep for 1 minute