creating a csv from information I have printed to screen

Ciarán Hudson ciaran.hudson at gmail.com
Thu May 21 17:04:58 EDT 2020


How do I edit the code below to create a csv file which includes the information I have printed at the bottom?

I'm pulling arrival data from an airport website and printing out only the flight info. I want to save that flight info to a CSV file.


import requests
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')

cells = soup.find_all('td')
#print(cells)

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=',')


More information about the Python-list mailing list