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 09:47:14 浏览: 64
这是一个使用 Python 爬取马士基航运网站船期信息的代码,具体来说,该代码使用了 requests 库和 BeautifulSoup 库对网页进行了爬取和解析,然后输出了船名、启运港、启运时间、目的港和到达时间等信息。其中,params 参数是请求参数,通过构造参数可以获取不同的船期信息,具体含义可以参考马士基航运网站的 API 文档。
相关问题
编写一个py爬虫脚本,每天爬取,https://www.reuters.com/ ;https://www.ap.org/en/ 新闻
好的,我们可以使用Python中的requests和BeautifulSoup库来编写爬虫脚本。以下是示例代码:
```python
import requests
from bs4 import BeautifulSoup
import datetime
# Reuters新闻
reuters_url = 'https://www.reuters.com/'
reuters_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.3'}
reuters_response = requests.get(reuters_url, headers=reuters_headers)
reuters_soup = BeautifulSoup(reuters_response.text, 'html.parser')
# AP新闻
ap_url = 'https://www.ap.org/en/'
ap_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.3'}
ap_response = requests.get(ap_url, headers=ap_headers)
ap_soup = BeautifulSoup(ap_response.text, 'html.parser')
# 保存新闻
now = datetime.datetime.now()
date = now.strftime('%Y-%m-%d')
with open(f'reuters_news_{date}.txt', 'w', encoding='utf-8') as f:
for article in reuters_soup.find_all('article'):
title = article.h3.text.strip()
f.write(title + '\n')
with open(f'ap_news_{date}.txt', 'w', encoding='utf-8') as f:
for article in ap_soup.find_all('article'):
title = article.h1.text.strip()
f.write(title + '\n')
```
该脚本将获取今天的《路透社》和AP通讯社的新闻标题,并将它们分别保存到名为`reuters_news_<date>.txt`和`ap_news_<date>.txt`的文件中,其中`<date>`是当前日期。您可以将此脚本添加到计划任务中,以每天自动运行并获取最新的新闻标题。
python爬取近七天的蔬菜价格信息,不要用http://www.xinfadi.com.cn网站
好的,您可以尝试使用其他的数据源,比如国家统计局发布的蔬菜价格信息。以下是一份示例代码,可以爬取近七天的菜价数据:
```python
import requests
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
# 获取当前日期
today = datetime.today().date()
# 构造日期列表
dates = [str(today - timedelta(days=i)) for i in range(7)]
# 构造请求url列表
urls = [
f'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/{date[:4]}/index.html'
for date in dates
]
# 发送请求并解析数据
for url in urls:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# TODO: 提取菜价数据并保存
```
这个示例代码中,我们使用了国家统计局的数据源,但是具体的数据提取和保存还需要您根据实际情况进行编写。