【爬虫】12306自动化购票

上文:

【爬虫】12306查票-CSDN博客

下面是简单的自动化进行抢票,只写到预定票,没有写完登陆,

跳出登陆后与上述代码同理修改即可。

【爬虫】12306自动化购票_第1张图片

感觉xpath最简单,复制粘贴:

【爬虫】12306自动化购票_第2张图片

 还有很多写法:

【爬虫】12306自动化购票_第3张图片

 官网地址:

简化写法 | DrissionPage官网

赋代码:

from DrissionPage import Chromium
from DrissionPage.common import Actions,Keys
from time import sleep

tab = Chromium(8898).latest_tab
ac = Actions(tab)

# 以下是自动化查询高铁票
tab.get('https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=%E5%B9%BF%E5%B7%9E%E5%8C%97,GBQ&ts=%E5%B9%BF%E5%B7%9E%E5%8D%97,IZQ&date=2025-05-20&flag=N,N,Y')
ac.move_to('x=//*[@id="fromStationText"]').click().type('guangzhoubei').type(Keys.ENTER)
sleep(1)
ac.move_to('x=//*[@id="toStationText"]').click().type('guangzhounan').type(Keys.ENTER)
sleep(1)
tab('x=//*[@id="train_date"]').clear()
ac.move_to('x=//*[@id="train_date"]').click().type('2025-05-23').type(Keys.ENTER)
ac.move_to('x=//*[@id="query_ticket"]').click()

ac.move_to('x=//*[@id="ticket_6c000G60350F_04_05"]/td[13]/a').click()

你可能感兴趣的:(爬虫,DrissionPage,python,爬虫)