Python unable to print titles by using requests and BeautifulSoup package

  beautifulsoup, python, python-3.x

I am trying to print out a list of all the article titles on the New York Times homepage by using python

https://www.nytimes.com/

My code ain’t working can some one plz explain what i did wrong

import requests
from bs4 import BeautifulSoup

url = 'https://www.nytimes.com/'
r = requests.get(url)
soup = BeautifulSoup(r.text)

for heading in soup.find_all(class_="story-heading"):
   if heading.a:
      print(heading.a.text.replace("n"," ").strip())
   else:
      print(heading.contents[0].strip())

Source: Python-3x Questions

LEAVE A COMMENT