You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用BeautifulSoup提取网页气象数据并保存为CSV文件

提取科特迪瓦SYNOPS气象数据并导出CSV

操作步骤

  • 先安装依赖包:pip install beautifulsoup4
  • 运行以下代码,即可解析你提供的HTML内容,提取气象数据并保存为CSV文件。

完整代码

from bs4 import BeautifulSoup
import csv

# 你提供的HTML内容
html_content = """
<p>SYNOPS from 65528, Odienne (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65528 42958 51202 10213 20208 39654 40126 85030 333 20209 58014 79999 85360=</p><p>202211061800 AAXX 06184 65528 11458 61404 10237 20214 39640 40108 69902 70296 83970 333 10326 58015 83815 81920 86360=</p><p>SYNOPS from 65536, Korhogo (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65536 42960 23402 10204 20204 39708 40122 80002 333 20203 58002 79999 82076=</p><p>202211061800 AAXX 06184 65536 11458 70402 10268 20204 39688 40095 69902 70162 83932 333 10340 58008 82613 82920 87076=</p><p>SYNOPS from 65545, Bondoukou (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65545 32958 ///// 10215 20206 39706 40124 80002 333 20212 59001 85076=</p><p>202211061800 AAXX 06184 65545 32958 ///// 10260 20213 39696 40107 80072 333 10325 58015 83360 88076=</p><p>SYNOPS from 65548, Man (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65548 42458 70000 10215 20215 39753 40121 86530 333 20215 59001 70140 86613=</p><p>202211061800 AAXX 06184 65548 31458 60000 10285 20234 39738 40097 71799 84933 333 10313 58011 84813 81913=</p><p>SYNOPS from 65555, Bouake (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65555 11440 32002 10210 20210 39698 40125 60084 71011 83502 333 20208 58003 83610=</p><p>202211061800 AAXX 06184 65555 11458 71402 10247 20237 39688 40109 60082 70196 8297/ 333 10315 58013 82813 81920 87360=</p><p>SYNOPS from 65557, Gagnoa (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65557 41458 41602 10211 20208 39881 40115 70296 84500 333 20211 59004 70250 84613=</p><p>202211061800 AAXX 06184 65557 13460 62204 10276 20234 39866 40094 69902 333 10320 58007 86613 81920=</p><p>SYNOPS from 65560, Daloa (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65560 32460 ///// 10220 20213 39811 40124 81572 333 20220 58002 81613 84360=</p><p>202211061800 AAXX 06184 65560 32460 ///// 10285 20219 39794 40100 83902 333 10324 58012 83815 81920 85076=</p><p>SYNOPS from 65562, Dimbokro (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65562 41209 60802 10230 20229 30017 40143 74442 86500 333 20222 58003 70090 83705 84613=</p><p>202211061800 AAXX 06184 65562 32460 53102 10305 20256 30003 40126 85900 333 10340 58016 85815 81920=</p><p>SYNOPS from 65563, Yamoussoukro (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65563 42956 30000 10208 20206 39882 40115 80001 333 20202 58004 70040 83076=</p><p>202211061800 AAXX 06184 65563 11458 70000 10234 20229 39868 40099 60042 79596 85903 333 10331 58014 85810 81920 87076=</p><p>SYNOPS from 65578, Abidjan (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65578 41358 60404 10245 20245 30112 40120 70362 83530 333 20244 58000 70010 81707 83620 86360= 202211061800 AAXX 06184 65578 32460 62006 10285 20254 30104 40112 85932 333 10297 58011 81917 85620 85075=</p><p>SYNOPS from 65585, Adiake (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65585 42458 ///// 10236 20232 30079 40117 82202 333 20232 58002 70080 82813 83076=</p><p>202211061800 AAXX 06184 65585 11460 ///// 10280 20247 30070 40108 60082 70262 85270 333 10315 58008 85813=</p><p>SYNOPS from 65592, Tabou (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65592 41458 71804 10239 20236 30097 40119 76266 8527/ 333 20239 58003 70150 85813 87360=</p><p>202211061800 AAXX 06184 65592 12460 71804 10276 20242 30083 40105 60052 83902 333 10300 58004 83813 81920 87076=</p><p>SYNOPS from 65594, San Pedro (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65594 42458 43604 10221 20221 30086 40120 82202 333 20220 58002 70100 82813 84080=</p><p>202211061800 AAXX 06184 65594 11458 53602 10271 20259 30071 40104 60102 70296 83202 333 10290 58002 83813 85080=</p><p>SYNOPS from 65599, Sassandra (Cote d'Ivoire)</p><p>202211070600 AAXX 07064 65599 41456 53202 10228 20224 30043 40118 70196 84202 333 20224 58000 70030 84813 84076=</p><p>202211061800 AAXX 06184 65599 31460 61802 10280 20252 30031 40104 70392 85902 333 10298 58001 85815 81920 84076=</p>
"""

# 解析HTML内容
soup = BeautifulSoup(html_content, 'html.parser')
all_p = soup.find_all('p')

# 存储最终提取的数据
final_data = []
current_station_id = ""
current_station_name = ""

# 遍历所有段落文本
for p in all_p:
    content = p.get_text(strip=True)
    if not content:
        continue
    
    # 识别站点信息行
    if content.startswith('SYNOPS from'):
        # 拆分站点ID和名称
        split_part = content.split(',', 1)
        current_station_id = split_part[0].replace('SYNOPS from ', '').strip()
        current_station_name = split_part[1].strip().replace(' (Cote d\'Ivoire)', '')
    else:
        # 处理气象数据,拆分同一行中的多条记录
        data_records = []
        if content.count('202211') > 1:
            # 按时间戳拆分多条数据
            timestamp_positions = [i for i, char in enumerate(content) if content.startswith('202211', i)]
            for idx in range(len(timestamp_positions)):
                if idx == len(timestamp_positions)-1:
                    record = content[timestamp_positions[idx]:].rstrip('=')
                else:
                    record = content[timestamp_positions[idx]:timestamp_positions[idx+1]].rstrip('=')
                data_records.append(record.strip())
        else:
            # 单条数据,去掉末尾的等号
            data_records.append(content.rstrip('=').strip())
        
        # 格式化每条数据并添加站点信息
        for record in data_records:
            fields = record.split()
            if current_station_id and current_station_name:
                final_data.append([current_station_id, current_station_name] + fields)

# 生成CSV表头
max_col_count = max(len(row) for row in final_data) if final_data else 0
csv_header = ['站点ID', '站点名称'] + [f'气象字段{i+1}' for i in range(max_col_count - 2)]

# 写入CSV文件
with open('科特迪瓦SYNOPS气象数据.csv', 'w', newline='', encoding='utf-8-sig') as file:
    writer = csv.writer(file)
    writer.writerow(csv_header)
    writer.writerows(final_data)

print("数据已成功导出到 科特迪瓦SYNOPS气象数据.csv")

代码说明

  1. HTML解析:使用BeautifulSoup提取所有<p>标签的文本内容,区分站点信息和气象数据。
  2. 数据拆分:针对Abidjan站点同一行包含两条数据的情况,按
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 15:30:49