利用selenium在12306上查询南京到上海2023年5月27日的列车,并在结果中筛选南京南站到上海虹桥站12:00-18:00的智能动车组,将筛选出的列车数量、历时最短车次和其出发、到达时间返回控制台
时间: 2024-01-24 22:19:30 浏览: 179
以下是利用Python和Selenium自动化查询12306上的列车信息的代码:
```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# 设置 Chrome 的无头模式
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--disable-gpu')
# 设置 Chrome 的驱动路径
service = Service('/path/to/chromedriver')
# 创建 Chrome 浏览器对象
driver = webdriver.Chrome(service=service, options=chrome_options)
# 打开 12306 网站
driver.get('https://www.12306.cn/index/')
# 点击“查询余票”
driver.find_element_by_link_text('查询余票').click()
# 输入出发城市
from_city = driver.find_element_by_id('fromStationText')
from_city.clear()
from_city.send_keys('南京')
from_city.send_keys(Keys.RETURN)
# 输入到达城市
to_city = driver.find_element_by_id('toStationText')
to_city.clear()
to_city.send_keys('上海')
to_city.send_keys(Keys.RETURN)
# 输入出发日期
date = driver.find_element_by_id('train_date')
date.clear()
date.send_keys('2023-05-27')
date.send_keys(Keys.RETURN)
# 等待查询结果加载完成
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'queryLeftTable')))
# 点击“智能筛选”
driver.find_element_by_link_text('智能筛选').click()
# 勾选“高速动车”
driver.find_element_by_id('cc_type_1').click()
# 勾选“南京南-上海虹桥”
select = Select(driver.find_element_by_id('cc_start_time'))
select.select_by_visible_text('12:00--18:00 南京南-上海虹桥')
# 等待筛选结果加载完成
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'queryLeftTable')))
# 获取符合条件的列车数量
num_trains = len(driver.find_elements_by_xpath('//tbody[@id="queryLeftTable"]/tr[not(@datatran)]'))
# 获取历时最短车次和其出发、到达时间
min_duration = float('inf')
min_duration_train = ''
min_duration_from_time = ''
min_duration_to_time = ''
for i in range(1, num_trains + 1):
train = driver.find_element_by_xpath(f'//tbody[@id="queryLeftTable"]/tr[not(@datatran)][{i}]')
train_name = train.find_element_by_class_name('train').text
duration = train.find_element_by_class_name('duration').text
from_time = train.find_element_by_class_name('start-t').text
to_time = train.find_element_by_class_name('color999').text
if '动车' in train_name and '南京南' in from_time and '上海虹桥' in to_time and '分钟' in duration:
duration = int(duration.split('分钟')[0])
if duration < min_duration:
min_duration = duration
min_duration_train = train_name
min_duration_from_time = from_time
min_duration_to_time = to_time
# 打印结果
print(f'符合条件的列车数量为:{num_trains}')
print(f'历时最短的列车为:{min_duration_train},历时{min_duration}分钟,从{min_duration_from_time}出发,到达{min_duration_to_time}')
```
需要注意的是,在运行代码之前需要先安装 Selenium 和 Chrome 驱动。另外,12306 网站的查询结果经常会变化,可能会导致代码失效。
阅读全文