barciewicz
barciewicz

Reputation: 3783

How to pretty print BeautifulSoup's string output

I have the following scraping code:

import requests, bs4

def make_soup():
    url = 'https://www.airbnb.pl/s/Girona--Hiszpania/homes?place_id=ChIJRRrTHsPNuhIRQMqjIeD6AAM&query=Girona%2C%20Hiszpania&refinement_paths%5B%5D=%2Fhomes&allow_override%5B%5D=&s_tag=b5bnciXv'
    response = requests.get(url)
    soup = bs4.BeautifulSoup(response.text, "html.parser")
    return soup

def get_listings():
    soup = make_soup()
    listings = soup.select('._f21qs6')
    number_of_listings = len(listings)
    print("Current number of listings: " + str(number_of_listings))
    while number_of_listings != 18:
        print("Too few listings: " + str(number_of_listings))
        soup = make_soup()
        listings = soup.select('._f21qs6')
        number_of_listings = len(listings)
    print("All fine! The number of listings is: " + str(number_of_listings))
    return listings

new_listings = get_listings()
print(new_listings)

I think def get_listings() returns listings as string, so I cannot use BeautifulSoup's prettify() on it and new_listings gets printed as one block of text.

Is there any way to print new_listings in HTML-esque format or at least have each tag printed at separate line?

Upvotes: 4

Views: 5705

Answers (2)

Amit Ghosh
Amit Ghosh

Reputation: 1606

Try:

from pprint import pprint 
pprint(new_listings)

pprint beautifies much outputs cleanly.

Upvotes: 0

ilja
ilja

Reputation: 2692

type(new_listings)
# list

Shows that new_listings is a list. Try:

print(new_listings[0].prettify())

Upvotes: 9

Related Questions