潮汐表查询 - 获取最新潮汐信息 - 潮汐预报
使用Python爬虫获取潮汐信息,以下代码示例可以帮助您从'https://www.chaoxibiao.net/'网站获取潮汐数据。
import requests
from bs4 import BeautifulSoup
# 设置请求头,模拟浏览器访问
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
# 发送请求获取页面内容
url = 'https://www.chaoxibiao.net/'
response = requests.get(url, headers=headers)
# 解析页面内容,提取潮汐信息
soup = BeautifulSoup(response.text, 'html.parser')
tides = soup.select('div.tide li')
for tide in tides:
time = tide.select_one('span.time').text.strip()
height = tide.select_one('span.height').text.strip()
print(time, height)
运行该代码即可获取网站'https://www.chaoxibiao.net/'上的潮汐信息。需要注意的是,如果该网站有反爬虫机制,可能需要使用代理或其他技术手段来解决。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/ofMH 著作权归作者所有。请勿转载和采集!