Search code examples
pythonbeautifulsoupexport-to-csv

Exporting to .csv from python with BeautifulSoup


I am quite new to this and cant seem to get this to export properly.

# select document
with open('scrape1.html') as html_file:
    soup = BeautifulSoup(html_file, 'lxml')

# create/name csv
with open('speechengine_report.csv', 'w') as csv_file:
    writer = csv.writer(csv_file)
    writer.writerow(['computer', 'usagedata']) 

# tell bs4 to only look at x tags with a class of y
for licensedata in soup.find_all('div', class_='licensedata'):

    # scrape pc id
    computer = licensedata.p.b.text
    print(computer)

    # scrape usage stats for each id
    for usagedata in licensedata.find_all('td'):

        # minutes = usagedata.table.tbody
        print(usagedata.text)

    # blank line
    print()

    # writer.writerow([computer, usagedata])

    
csv_file.close()

Solution

  • Rest of the code wherein you want to write data to your csv file should be within the with block. Also, you don't need csv_file.close() since with handles that for you. Try the below code. Read file handling in python

    with open('scrape1.html') as html_file:
        soup = BeautifulSoup(html_file, 'lxml')
    
    # create/name csv
    with open('speechengine_report.csv', 'w') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(['computer', 'usagedata']) 
        # tell bs4 to only look at x tags with a class of y
        for licensedata in soup.find_all('div', class_='licensedata'):
    
            # scrape pc id
            computer = licensedata.p.b.text
            print(computer)
    
            # scrape usage stats for each id
            for usagedata in licensedata.find_all('td'):
    
            # minutes = usagedata.table.tbody
                print(usagedata.text)
    
            # blank line
            print()
    
            # writer.writerow([computer, usagedata])