BeautifulSoup and prettify() function

17,661

try this code:

import requests
from bs4 import BeautifulSoup
response = requests.get("https://www.doviz.com")
soup = BeautifulSoup(response.text, "html.parser")
print(soup.prettify())
Share:
17,661
Goktug
Author by

Goktug

Updated on June 04, 2022

Comments

  • Goktug
    Goktug almost 2 years

    To parse html codes of a website, I decided to use BeautifulSoup class and prettify() method. I wrote the code below.

    import requests
    import bs4
    
    response = requests.get("https://www.doviz.com")
    soup = bs4.BeautifulSoup(response.content, "html.parser")
    print(soup.prettify())
    

    When I execute this code on Mac terminal, indentation of the codes are not set. On the other hand, If I execute this code on windows cmd or PyCharm, all codes are set.

    Do you know the reason for this ?

  • Mew
    Mew over 2 years
    Or, in words: construct the soup using response.text, not response.content.