I've been trying to format terminal output in line with this stack.
Yet for some reason, the only method that works is the use of columnar but that's limited to showing only 3 rows of text.
I've tried almost all of the methods and yet I almost aways get an output that looks like this:
[Payne, Roberts and Davis, Vasquez-Davidson, Jackson, Chambers and Levy, Savage-Bradley, Ramirez Inc, Rogers-Yates, Kramer-Klein, Meyers-Johnson, Hughes-Williams, Jones, Williams and Villa, Garcia PLC, Gregory and Sons, Clark, Garcia and Sosa, Bush PLC, Salazar-Meyers, Parker, Murphy and Brooks, Cruz-Brown, Macdonald-Ferguson, Williams, Peterson and Rojas, Smith and Sons, Moss, Duncan]
I've been trying to learn how to scrape a website and display the output in a readable format.
import requests
from bs4 import BeautifulSoup
from columnar import columnar
import numpy as np
URL = "https://realpython.github.io/fake-jobs/"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find(id="ResultsContainer")
job_elements = results.find_all("div", class_="card-content")
title = []
company = []
location = []
for job_element in job_elements:
title_element = job_element.find("h2", class_="title")
company_element = job_element.find("h3", class_="company")
location_element = job_element.find("p", class_="location")
formatted_title_element = title_element.text.strip()
formatted_company_element = company_element.text.strip()
formatted_location_element = location_element.text.strip()
title.append(formatted_title_element)
company.append(formatted_company_element)
location.append(formatted_location_element)
data = []
data.append(title)
data.append(company)
data.append(location)
headers = ['Title', 'Company', 'Location']
table = columnar(data, headers, no_borders=True)
print(table)
The columar solution above is the only one from that stack that doesn't automatically format things in the example at the top of the question but again, it only outputs 3 lines. Columnar does have a variable head=x which is meant to show x number of rows, however when I use the switch I get the same output as in the example at the very top.