scrape headline from new york times in python

Here's a code snippet in Python that uses requests and Beautiful Soup libraries to scrape the latest headline from the New York Times website:

main.py
import requests
from bs4 import BeautifulSoup

url = 'https://www.nytimes.com/'
response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')
headline = soup.find('h2', {'class': 'css-1vvhd4r esl82me1'}).text.strip()

print(headline)
253 chars
11 lines

Note: The class name css-1vvhd4r esl82me1 may change over time as the website gets updated. So before running the code, it's recommended to inspect the HTML code of the website and update the class name accordingly.

gistlibby LogSnag