Unable to understand empty array output while using Beautiful Soup

Sid :

I have written a very small python script that scrapes article headlines from CNN's website.

import requests
from bs4 import BeautifulSoup

url='https://edition.cnn.com/'
topics=['world','politics','business']
r=requests.get(url+topics[1])
soup=BeautifulSoup(r.content,'html.parser')
spans=soup.find_all('span',{'class':"cd__headline-text"})
print(spans)

Upon execution of this code I am simply getting an empty list as an output.This is not what I was expecting or looking for as I am trying to scrape the text that follows after the tag. The snippet of html block that I am trying to refer to is-

<span class="cd__headline-text">
Bernie Sanders faces pivotal clash as Democratic establishment joins forces against him
</span>

Please help clarify what my code seems to be doing wrong and/or any logical errors that I might be making.

petezurich :

Your code runs fine. It just doesn't yield results for the politics page.

Try this:

import requests
from bs4 import BeautifulSoup

url='https://edition.cnn.com/'
topics = ['world','politics','business']

headlines = []

for topic in topics:

    r = requests.get(url+topic)
    soup=BeautifulSoup(r.content,'html.parser')

    for span in soup.find_all('span',{'class':"cd__headline-text"}):
        headlines.append(span.text)
        print(span.text)
        print()

headlines prints out to:

The bizarre ways that coronavirus is changing etiquette
Over half of all virus cases in one country are linked to this group
Trump's Middle East plan could jeopardize Jordan-Israel peace treaty, Jordan PM says
Irish duo's win marks rare victory for women in the 'Nobel of architecture'
After more than 240 days, Australia's New South Wales is finally free from bushfires
Child drowns off Greek coast after Turkey opens border with Europe 
A migration crisis and disagreement with Turkey is the last thing Europe needs right now
Vatican to open controversial WW2-era files on Pope Pius XII
Netanyahu projected to win Israeli election, but exit polls suggest bloc just short of majority
Adviser to Iran's Supreme Leader dies after contracting coronavirus
Israeli election exit polls project Netanyahu in lead
She became pregnant at the age of 12. Now, Kenya's Christine Ongare is an Olympic boxing qualifier
Nigeria says it is ready and more than capable of dealing with coronavirus
Kenya bans commercial slaughter of donkeys following a rise in animal theft 
Violence forces Haiti to cancel Carnival
....

You don't get results for politics because the content is rendered dynamically with Javascript in the browser (as G. Anderson explained in his comments). With requests however you only get the raw HTML.

Open the site in the browser and compare View page source with Inspect element. The former yields the raw HTML the latter the rendered HTML.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23780&siteId=1