写了一整天,代码设置起始站,终点站,出行日期,通过爬虫从12306爬取选择当日的车票信息,保存在csv文件中,随后通过邮箱将包含车次信息的csv文件发送到个人邮箱账号,个人阅读后回发一个邮件,期间包含车次信息,电脑进入邮箱读取邮件,获得所选车次,进行自动化订票,期间需要输入一次验证码,目前是控制台输入,也可以邮箱发送给电脑,这样就可以远程订票了。
部分代码
# 提取数据+保存为csv
def request_csv():
response = requests.get(url=url, headers=headers)
response.encoding = 'utf8'
data_json = response.json()
#提取+保存
f = open('dic_12306.csv','w',encoding='utf-8',newline='')
csv_writer = csv.writer(f)
csv_writer.writerow(dic_12306.keys())
result = data_json['data']['result']
for i in result:
index = i.split('|')
dic_12306['车次'] = index[3]
dic_12306['出发时间'] = index[8]#出发时间
dic_12306['到达时间'] = index[9]#到达时间
dic_12306['消耗时间'] = index[10]#消耗时间
dic_12306['头等座'] = index[32]#头等座
dic_12306['一等座'] = index[31]#一等座
dic_12306['二等座'] = index[30]#二等座
dic_12306['硬卧'] = index[28]#硬卧
dic_12306['硬座'] = index[29]#硬座
dic_12306['无座'] = index[26]#无座
dic_12306['软卧'] = index[23]#软卧
csv_writer.writerow(dic_12306.values())
f.close()
response.close()
return ''
# 购票
def buy_card(index):
# 打开浏览器,正确的实例化方式是使用括号
driver = webdriver.Edge() # 注意这里的括号
# 最大化
driver.maximize_window()
# number = input('请输入手机号')
# pass_word = input('请输入密码')
# id_card = input('请输入身份证后四位')
# 访问网站
driver.get(url='https://kyfw.12306.cn/otn/leftTicket/init')
sleep(2)
# # 输入出发地
# # 先找到出发地输入框
driver.find_element(by='id', value='fromStationText').click()
sleep(0.5)
driver.find_element(by='id', value='fromStationText').clear()
sleep(0.5)
driver.find_element(by='id', value='fromStationText').send_keys('葫芦岛北')
sleep(0.5)
driver.find_element(by='id', value='fromStationText').send_keys(Keys.ENTER)
# 目的地
driver.find_element(by='id', value='toStationText').click()
sleep(0.5)
driver.find_element(by='id', value='toStationText').clear()
sleep(0.5)
driver.find_element(by='id', value='toStationText').send_keys('沈阳')
sleep(0.5)
driver.find_element(by='id', value='toStationText').send_keys(Keys.ENTER)
# 日期
driver.find_element(By.ID, value='train_date').click()
sleep(0.5)
driver.find_element(By.ID, value='train_date').clear()
sleep(0.5)
driver.find_element(By.ID, value='train_date').send_keys('2024-10-27')
sleep(0.5)
driver.find_element(By.ID, value='train_date').send_keys(Keys.ENTER)
# 查询
driver.find_element(By.ID, value='query_ticket').click()
sleep(5)
# 点击预订
driver.find_element(By.XPATH, f'//*[@id="queryLeftTable"]/tr[{index}]/td[@class="no-br"]/a').click()
sleep(2)
# 登录
driver.find_element(By.ID, value='J-userName').click() # 手机号
driver.find_element(By.ID, value='J-userName').clear()
driver.find_element(By.ID, value='J-userName').send_keys(number)
driver.find_element(By.ID, value='J-userName').send_keys(Keys.ENTER)
sleep(0.5)
driver.find_element(By.ID, value='J-password').click() # 密码
driver.find_element(By.ID, value='J-password').clear()
driver.find_element(By.ID, value='J-password').send_keys(pass_word)
driver.find_element(By.ID, value='J-password').send_keys(Keys.ENTER)
sleep(0.5)
# 立即登录
driver.find_element(By.ID, value='J-login').click()
sleep(2)
# 输入身份证号后四位
driver.find_element(By.ID, value='id_card').click() # 密码
driver.find_element(By.ID, value='id_card').clear()
driver.find_element(By.ID, value='id_card').send_keys(id_card)
driver.find_element(By.ID, value='id_card').send_keys(Keys.ENTER)
sleep(0.5)
# 获取验证码
driver.find_element(By.ID, value='verification_code').click()
sleep(2)
# 输入验证码
code = input('输入验证码')
driver.find_element(By.ID, value='code').click() # 输入验证码
driver.find_element(By.ID, value='code').send_keys(code)
driver.find_element(By.ID, value='code').send_keys(Keys.ENTER)
sleep(2)
# 确定
driver.find_element(By.ID, value='sureClick').click() # 输入验证码
sleep(2)
# 选择乘车人
driver.find_element(By.ID, value='normalPassenger_0').click()
sleep(2)
# 选择是否学生票
# select_ = input('选择是否为学生票:')
if select_ == '否':
driver.find_element(By.ID, value='dialog_xsertcj_cancel').click()
elif select_ == '是':
driver.find_element(By.ID, value='dialog_xsertcj_ok').click()
sleep(2)
# 提交订单
driver.find_element(By.ID, value='submitOrder_id').click()
sleep(2)
# 确认信息
driver.find_element(By.ID, value='qr_submit_id').click()
sleep(2)
# 是否确认购买
# y_or_no = input('是否确认购买:')
if y_or_no == '是':
driver.find_element(By.ID, value='qr_submit_id').click()
elif y_or_no == '否':
driver.find_element(By.ID, value='back_edit_id').click()
print('购买成功')
driver.close()
if __name__ == "__main__":
# #输入起始站
# start_station = input('请输入起始站:')
# # 输入终点站
# end_station = input('请输入终点站:')
#爬取数据
request_csv()
print('爬取完成')
#发送爬取的csv文件到邮箱
print('开始发送数据')
send_phone()
print('发送完毕')
#打开邮箱读取主题部分
num = get_email()
# break
#根据索引寻找对应的车次
buy_card(2*(num+1)-1)
运行结果:
标签:python,抢票,driver,value,element,12306,sleep,ID,find From: https://blog.csdn.net/chu_kuang_/article/details/143195036