import requestsfrom bs4 import BeautifulSoupfrom datetime import datetimeurl = "https://www.maersk.com.cn/schedules/pointToPoint"today = datetime.today().strftime("%Y-%m-%d")params = { "from": "2IW9P6J7XAW72", "to": "0XOP5ISJZK0HR", "containerIsoCode": "42G1", "fromServiceMode": "CY", "toServiceMode": "CY", "numberOfWeeks": "4", "dateType": "D", "date": today, # 将当前日期格式化为字符串,并作为请求参数传递 "vesselFlag": "",}response = requests.get(url, params=params)soup = BeautifulSoup(response.text, "html.parser")for row in soup.select(".table-row"): departure_port = row.select_one(".departure-port").text.strip() departure_time = row.select_one(".departure-time").text.strip() arrival_port = row.select_one(".arrival-port").text.strip() arrival_time = row.select_one(".arrival-time").text.strip() vessel_name = row.select_one(".vessel-name").text.strip() print(f"船名:{vessel_name},启运港:{departure_port},启运时间:{departure_time},目的港:{arrival_port},到达时间:{arrival_time}")
时间: 2023-07-19 07:47:14 浏览: 156
这是一个使用 Python 爬取马士基航运网站船期信息的代码,具体来说,该代码使用了 requests 库和 BeautifulSoup 库对网页进行了爬取和解析,然后输出了船名、启运港、启运时间、目的港和到达时间等信息。其中,params 参数是请求参数,通过构造参数可以获取不同的船期信息,具体含义可以参考马士基航运网站的 API 文档。
阅读全文