现在我想将service添加一下输入列表以避免非法字符你可以帮我完善一下么,列表如下:<select id="service" name="service" class="form-control"><option value="RUFEX">RUFEX</option><option value="MED">MED</option><option value="EU">EU</option><option value="CIMEX">CIMEX</option><option value="RED SEA">RED SEA</option><option value="INPA">INPA</option><option value="CNC">CNC</option><option value="AFRICA">AFRICA</option><option value="TP">TP</option><option value="LATAM">LATAM</option><option value="ANL">ANL</option></select>
时间: 2023-07-19 17:54:53 浏览: 186
好的,可以按照您的要求对代码进行修改,将可选服务列表添加到程序中,让用户可以从列表中选择服务而不是手动输入。以下是修改后的代码:
```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')
service_select = soup.find('select', {'id': 'service'})
service_options = service_select.find_all('option')
service_list = [option['value'] for option in service_options]
# 用户选择服务
print('可选服务如下:')
for index, service in enumerate(service_list):
print(f'{index + 1}. {service}')
selected_index = int(input('请选择服务序号:'))
selected_service = service_list[selected_index - 1]
# 获取起始港口列表
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': selected_service,
'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('请求失败')
```
现在用户可以从可选服务列表中选择服务,而不是手动输入服务名称。
阅读全文