这个列表是pol的输入列表你能帮我添加进去么,<select id="pol" name="pol" class="form-control"><option value="CNNSA">NANSHA</option><option value="CNSWA">SHANTOU</option><option value="CNSHA">SHANGHAI</option><option value="CNTXG">TIANJIN XINGANG</option><option value="CNDLC">DALIAN</option><option value="CNTSN">TIANJIN</option><option value="TWTXG">TAICHUNG</option><option value="TWKHH">KAOHSIUNG</option><option value="CNCAN">GUANGZHOU</option><option value="CNYTN">YANTIAN</option><option value="CNSZX">SHENZHEN</option><option value="CNLYG">LIANYUNGANG</option><option value="TWTYN">TAOYUAN</option><option value="CNFUG">FUQING, FUZHOU, 35</option><option value="TWTPE">TAIPEI</option><option value="CNXMN">XIAMEN</option><option value="CNNGB">NINGBO</option><option value="TWKEL">KEELUNG</option><option value="CNTAO">QINGDAO</option><option value="HKHKG">HONG KONG SAR, CHINA</option><option value="CNSHK">SHEKOU</option><option value="CNFOC">MAWEI FUZHOU</option></select>
时间: 2023-07-19 21:54:54 浏览: 201
当用户需要选择起始港口时,我们可以将对应的选项值传递给`payload`字典中的`pol`键,以获取相应的数据。下面是一个示例代码,其中包含了所有可选的起始港口:
```python
import requests
from bs4 import BeautifulSoup
# 获取起始港口列表
url = 'https://www.cma-cgm.com.cn/LaraToolkit/ShipScheduleInquery/SearchList'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
pol_select = soup.find('select', {'id': 'pol'})
pol_options = pol_select.find_all('option')
pol_list = [option['value'] for option in pol_options]
# 用户选择起始港口
print('可选起始港口如下:')
for index, pol in enumerate(pol_list):
print(f'{index + 1}. {pol}')
selected_index = int(input('请选择起始港口序号:'))
selected_pol = pol_list[selected_index - 1]
# 构造请求
url = 'https://www.cma-cgm.com.cn/LaraToolkit/ShipScheduleInquery/PostListData'
payload = {
'vessel': '',
'voyage': '',
'service': 'MED',
'pol': selected_pol,
'area': '',
'etdDateFrom': '2023-06-09',
'etdDateTo': '2023-07-08',
'line': '',
'Draw': '2',
'Page': '1',
'Rows': '10',
'order': 'asc',
'sort': ''
}
# 发送请求
response = requests.post(url, data=payload)
# 解析响应
if response.status_code == 200:
data = response.json()['data']
for item in data:
pol_name = item['POLName']
vessel_code = item['VesselCode']
etd_date = item['EtdDate']
si_cut_off = item['SiCutOff']
print(pol_name, vessel_code, etd_date, si_cut_off)
else:
print('请求失败')
```
在上述代码中,我们首先通过 BeautifulSoup 库解析页面,获取所有可选的起始港口,并将其存储在`pol_list`列表中。然后通过用户输入选择起始港口,将其传递给`payload`字典中的`pol`键,再发送 POST 请求,获取相应的数据。
阅读全文