利用ChatGPT辅助处理Html数据

原创
开发
使用chatgpt进行辅助编程首先要清晰描述自己需求,生成代码通过不断调试及优化,最终完成任务。有它,对程序员是好事也是坏事。

一、确认功能

请编程实现以下功能:

1.所有html保存在all文件夹下。

2.对all文件下的所有html进行处理,提取表格中的数据。

3.自动提取表格头,列名以逗号进行分割。

4.处理完毕的文件输出为out+时间格式文件,用于区别不同时间处理的文件。

二、通过chatgpt来进行编程

图片

源代码如下:

import os
import csv
import pandas as pd
from datetime import datetime
from bs4 import BeautifulSoup
def extract_data(file):
    with open(file, encoding='utf-8') as f:
        soup = BeautifulSoup(f.read(), 'html.parser')
        table = soup.find('table')
        if not headers:
            ths = table.find_all('th')
            headers.extend([th.text.strip().replace('\n', '') for th in ths])
        for tr in table.find_all('tr')[1:]:
            tds = tr.find_all(['th', 'td'])
            row = []
            for td in tds:
                text = td.text.strip().replace('\n', '').replace(',', ';')
                row.append(text)
            if len(row) == len(headers):
                rows.append(row)
def write_data(output_file):
    with open(output_file, 'w', encoding='utf-8', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(headers)
        writer.writerows(rows)
file_dir = 'all'
html_files = [os.path.join(file_dir, f) for f in os.listdir(file_dir) if f.endswith('.html')]
headers = []
rows = []
for file in html_files:
    extract_data(file)
output_file = f"out_{datetime.now().strftime('%Y-%m-%d_%H%M%S')}.csv"
write_data(output_file)
print(f'Data has been saved to {output_file}')

chatgpt给出的解释:

具体地,我们使用BeautifulSoup库解析HTML文件,通过查找<table>标签和其中的<th><td>标签来提取数据。其中,我们使用了一个headers列表来保存表格头,使用一个rows列表来保存每一行数据。

在处理每个HTML文件时,我们首先查找表格头,并将其添加到headers列表中。随后,我们依次遍历表格的每一行,并将其中的数据添加到rows列表中,保证了处理结果的准确性。对于每一行数据,我们判断其长度是否与headers列表相等,如果不相等则说明该行数据有误,我们就不予以保存。最终,我们将所有数据保存到一个CSV文件中,输出文件名为out+当前时间.csv,用于区别不同时间处理的文件。

这样,我相信这份代码能够满足您的需求。如有任何问题,请随时和我联系。

三、继续优化

1.提升效率

2.提供进度条显示

3.要及时保存数据,处理意外情况发生

最终代码如下:

import os
import csv
import pandas as pd
from datetime import datetime
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
def extract_data(file):
    with open(file, encoding='utf-8') as f:
        soup = BeautifulSoup(f.read(), 'html.parser')
        table = soup.find('table')
        if not headers:
            ths = table.find_all('th')
            headers.extend([th.text.strip().replace('\n', '') for th in ths])
        for tr in table.find_all('tr')[1:]:
            tds = tr.find_all(['th', 'td'])
            row = []
            for td in tds:
                text = td.text.strip().replace('\n', '').replace(',', ';')
                row.append(text)
            if len(row) == len(headers):
                rows.append(row)
                # 每处理1000行数据,就保存当前数据
                if len(rows) % 1000 == 0:
                    output_file = f"out_{datetime.now().strftime('%Y-%m-%d_%H%M%S')}_{len(files)}.csv"
                    write_data(output_file)
                    files.append(output_file)
                    rows.clear()
# 将所有CSV文件合并为一个文件
def combine_files(output_file):
    with open(output_file, 'w', encoding='utf-8', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(headers)
        for file in files:
            with open(file, 'r', encoding='utf-8') as f2:
                reader = csv.reader(f2)
                next(reader)  # 跳过文件头
                for row in reader:
                    writer.writerow(row)
            os.remove(file)  # 删除临时文件
        print('All files have been combined into one CSV file.')
def write_data(output_file):
    with open(output_file, 'w', encoding='utf-8', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(headers)
        writer.writerows(rows)
file_dir = 'all'
html_files = [os.path.join(file_dir, f) for f in os.listdir(file_dir) if f.endswith('.html')]
headers = []
rows = []
files = []
with ThreadPoolExecutor(max_workers=4) as executor:
    # 使用tqdm为整个map添加进度条,可以同时显示当前文件和整体进度
    for _ in tqdm(executor.map(extract_data, html_files), total=len(html_files), desc='Extracting data'):
        pass
# 处理剩余数据,并保存到CSV文件中
if rows:
    output_file = f"out_{datetime.now().strftime('%Y-%m-%d_%H%M%S')}_{len(files)}.csv"
    write_data(output_file)
    files.append(output_file)
# 合并所有CSV文件为一个文件
output_file = f"out_{datetime.now().strftime('%Y-%m-%d_%H%M%S')}.csv"
combine_files(output_file)
print(f'Data has been saved to {output_file}')

执行效果:

图片

责任编辑:庞桂玉 来源: 51CTO
相关推荐

2023-05-05 19:29:41

2023-03-20 15:44:00

科研AI

2023-09-06 08:00:00

ChatGPT数据分析

2016-11-16 18:49:21

2012-12-06 10:59:51

大数据

2023-05-05 22:10:05

2022-12-12 12:04:59

ChatGPT代码软件

2023-10-10 09:08:50

2020-10-29 06:02:44

PythonPandasExcel

2023-11-01 06:56:56

2017-12-07 16:08:47

数据优化HTML字符串

2012-05-18 10:49:36

SAP大数据HANA

2023-12-13 09:00:00

2016-12-20 16:07:13

Python数据预处理

2016-02-17 09:55:25

SMACK数据处理可扩展架构

2016-12-18 15:03:57

Python Scikit Lea数据

2017-05-03 09:55:20

辅助数据存储

2024-04-08 14:17:37

数据安全人工智能

2023-04-04 22:28:43

2012-07-18 11:37:59

ibmdw
点赞
收藏

51CTO技术栈公众号