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

如何用Python拆分网站表格中的潮汐与海浪数据?

解决方案:拆分海浪与潮汐数据爬取并导入Google Sheets

前置依赖安装

先安装所需Python库:

pip install requests beautifulsoup4 gspread oauth2client

1. 爬取海浪数据(第一个表格)脚本

定位页面第一个表格提取海浪数据,支持导出CSV或直接导入Google Sheets指定标签页:

import requests
from bs4 import BeautifulSoup
import gspread
from oauth2client.service_account import ServiceAccountCredentials

# 目标页面URL
URL = "https://magicseaweed.com/Wildwood-Surf-Report/392/"

# 获取页面内容
response = requests.get(URL)
soup = BeautifulSoup(response.text, "html.parser")

# 定位第一个表格(海浪数据)
wave_table = soup.find_all("table")[0]

# 提取表头
headers = [th.text.strip() for th in wave_table.find("thead").find_all("th")]

# 提取表格行数据
rows = []
for row in wave_table.find("tbody").find_all("tr"):
    row_data = [td.text.strip() for td in row.find_all("td")]
    rows.append(row_data)

# --- 可选:导出为本地CSV文件 ---
# import csv
# with open("wave_data.csv", "w", newline="", encoding="utf-8") as f:
#     writer = csv.writer(f)
#     writer.writerow(headers)
#     writer.writerows(rows)

# --- 导入到Google Sheets指定标签页 ---
# 配置API权限(需提前创建Google服务账号并下载密钥文件)
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("your-service-account-key.json", scope)
client = gspread.authorize(creds)

# 打开目标文档,选择"海浪数据"标签页
sheet = client.open("你的Google Sheets文档名").worksheet("海浪数据")

# 清空原有数据(可选)
sheet.clear()

# 写入表头和数据
sheet.insert_row(headers, 1)
sheet.insert_rows(rows, 2)

2. 爬取潮汐数据(第二个表格)脚本

复用核心逻辑,定位第二个表格提取潮汐数据:

import requests
from bs4 import BeautifulSoup
import gspread
from oauth2client.service_account import ServiceAccountCredentials

# 目标页面URL
URL = "https://magicseaweed.com/Wildwood-Surf-Report/392/"

# 获取页面内容
response = requests.get(URL)
soup = BeautifulSoup(response.text, "html.parser")

# 定位第二个表格(潮汐数据)
tide_table = soup.find_all("table")[1]

# 提取表头
headers = [th.text.strip() for th in tide_table.find("thead").find_all("th")]

# 提取表格行数据
rows = []
for row in tide_table.find("tbody").find_all("tr"):
    row_data = [td.text.strip() for td in row.find_all("td")]
    rows.append(row_data)

# --- 可选:导出为本地CSV文件 ---
# import csv
# with open("tide_data.csv", "w", newline="", encoding="utf-8") as f:
#     writer = csv.writer(f)
#     writer.writerow(headers)
#     writer.writerows(rows)

# --- 导入到Google Sheets指定标签页 ---
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("your-service-account-key.json", scope)
client = gspread.authorize(creds)

# 打开目标文档,选择"潮汐数据"标签页
sheet = client.open("你的Google Sheets文档名").worksheet("潮汐数据")

# 清空原有数据(可选)
sheet.clear()

# 写入表头和数据
sheet.insert_row(headers, 1)
sheet.insert_rows(rows, 2)

关键说明

  • 表格拆分:通过find_all("table")[0]和[1]精准定位目标表格,解决数据混排问题
  • Google Sheets配置:需在Google Cloud平台创建服务账号,下载密钥文件并替换代码中的文件名,同时给服务账号授予目标表格的编辑权限
  • 每日自动执行:可结合系统定时任务(如Linux的cron、Windows任务计划程序)实现全年每日自动爬取

内容的提问来源于stack exchange,提问作者Anthony Madle

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 03:46:14