[Python] Web scraping with requests not working correctly

Discussão em 'Python' iniciado por Stack, Abril 30, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to get the html from CNN for a personal project. I am using the requests library and am new to it. I have followed basic tutorials to get the HTML from CNN using requests, but keep getting responses that are different from the HTML I find when I inspect the webpage from my browser. Here is my code:

    base_url = 'https://www.cnn.com/'
    r = requests.get(base_url)
    soup = BeautifulSoup(r.text, "html.parser")
    print(soup.prettify())


    I am trying to get article titles from CNN, but this is my first issue. Thanks for the help!

    Update It seems that I know even less than I had initially assumed. My real question is: How do I extract titles from the CNN homepage? I've tried both answers, but the HTML from requests does not contain title information. How can I get the title information like what is in this picture (Screenshot of my browser)Screenshot of cnn article title with accompanying html side by side

    Continue reading...

Compartilhe esta Página