How to write output of web scrape to column instead of rows in Beautiful Soup
Question: I'm trying to write the results of scraping a webpage to a CSV file. I have
successfully written the output to CSV but it went in as rows instead of
columns. Here is the script:

    
    
    import bs4
    import requests
    import csv
    
    #get webpage for Apple inc. September income statement
    page = requests.get("https://au.finance.yahoo.com/q/is?s=AAPL")
    
    #put into beautiful soup
    soup = bs4.BeautifulSoup(page.content)
    
    #select table that holds data of interest
    table = soup.find("table", class_="yfnc_tabledata1")
    
    #creates generator that holds four values that are yearly revenues for company
    revenue = table.tr.td.table.tr.next_sibling.td.next_siblings
    
    #iterates through generator from above and writes output to CSV file
    for value in revenue:
        value = value.get_text(strip=True)
        with open('/home/kwal0203/Desktop/Apple.csv', 'a') as csvfile:
                s = csv.writer(csvfile)
                s.writerow([data.encode("utf-8") for data in [value]])
    

I understand there is a `zip()` function in Python that may be of use but
haven't been able to figure out how I can apply it to this situation.

Thanks any help appreciated.


Answer: You have the right idea, **zip** can easily help you here:

    
    
    #creates generator that holds four values that are yearly revenues for company
    revenue = table.tr.td.table.tr.next_sibling.td.next_siblings
    
    revenue = zip(*revenue) # <------ yes, it is that easy
    
    #iterates through generator from above and writes output to CSV file
    for value in revenue:
        value = value.get_text(strip=True)
             ...
    

