首页 > 解决方案 > 使用 BeautifulSoup 从 python 导出到 .csv

问题描述

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

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

标签: pythonbeautifulsoupexport-to-csv

解决方案


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


推荐阅读