# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import requests
from bs4 import BeautifulSoup


#grab the NYT homepage
response = requests.get("http://www.nytimes.com")

#feed t into BSP
doc = BeautifulSoup(response.text, 'html.parser')

#get out the stories 
stories = doc.find_all("article", {'class': 'story'})
len(stories)

# +
all_stories = []
#grab the headlines and bylines
for story in stories:
    headline = story.find('h2', {'class':'story-heading'})
    if headline:
        this_story= {'headlines': headline_text}
        headline_text = headline.text.strip()
        byline = story.find('p', {'class': 'byline'})
        if byline:
            by_line_text = byline.text.strip()
        all_stories.append(this_story)    
    
all_stories

# +
#ave the headlines and bylines to a timestamped CSV
# -

# !pip install pandas

import pandas as pd

stories_df = pd.DataFrame(all_stories)
stories_df.head()

stories_df.to_csv("nyt-data.cvs")

import time

datastring =  time.strftime("%Y-%m-%d-%I-%M-%p")
datastring

filename = "nyt-data-" + datastring + ".csv"
stories_df.to_csv(filename, index=False)



StarcoderdataJupyterScriptsDedupFiltered