This has helped a lot; cleaner output, keeping tbl format and creating a csv but the csv is not being populated. Any idea what I am doing wrong?
import requests import csv, sys from bs4 import BeautifulSoup cookies = { 'ApplicationGatewayAffinity': '1d2ad8ab214d1293a4e31bcd161589fa82a54a39bb7b3be80b503996092d4296', 'ApplicationGatewayAffinityCORS': '1d2ad8ab214d1293a4e31bcd161589fa82a54a39bb7b3be80b503996092d4296', } headers = { 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Sec-Fetch-Site': 'cross-site', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-User': '?1', 'Sec-Fetch-Dest': 'document', 'Referer': 'https://www.google.com/', 'Accept-Language': 'en-GB,en;q=0.9,en-US;q=0.8,fr;q=0.7,nl;q=0.6', } response = requests.get('https://www.corkairport.com/arrivals-departures', headers=headers, cookies=cookies) #print(response.content) soup = BeautifulSoup(response.content, 'html.parser') #print(soup.prettify()) ## commented out as it did not print as expected # headers = soup.find_all('th') # for header in headers: # for content in header.contents: # value = str(header).strip().replace('\n', '') # if len(value) == 0: # print('"0"', end=',') # elif value[0].lower() in 'abcdefghijklmnopqrstuvwxyz<': # print('\n' + value, end=',') # else: # print('"' + value + '"', end=',') #print('Arriving From', 'Airline', 'Scheduled to arrive','Latest Update','Status') writer = csv.writer(sys.stdout) writer.writerow([ 'Arriving From' 'Airline', 'Scheduled to arrive', 'Latest Update', 'Status' ]) #cells = soup.find_all('td') #print(cells) [table] = soup.find_all("table") for row in table.find_all("tr"): writer.writerow( [td.string.strip() for td in row.find_all("td")] ) # for cell in cells: # for content in cell.contents: # value = str(content).strip().replace('\n', '') # if len(value) == 0: # print('"0"', end=',') # elif value[0].lower() in 'abcdefghijklmnopqrstuvwxyz<': # print('\n' + value, end=',') # else: # print('"' + value + '"', end=',') with open('flight.csv','w') as f: writer = csv.writer(f) -- https://mail.python.org/mailman/listinfo/python-list