使用 BeautifulSoup 从 python 导出到 .csv

Exporting to .csv from python with BeautifulSoup

我对此很陌生,似乎无法正确导出它。

# 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()

您要将数据写入 csv 文件的其余代码应位于 with 块中。此外,您不需要 csv_file.close() 因为 with 可以为您处理。试试下面的代码。阅读 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])