12306练习
webdriver.Chome()要放在全局,放在类当中会触发垃圾回收机制
当登录的时候,为了方便查看,用显示等待,当触发了页面包含(url_contains),打印登录成功
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# 登录
driver = webdriver.Chrome()
class TrainSpider(): # 出发地、目的地、日期
login_url = 'https://kyfw.12306.cn/otn/resources/login.html'
personal_url ='https://kyfw.12306.cn/otn/view/index.html'
def __init__(self, from_station, to_station, train_date):
self.from_station = from_station
self.to_station = to_station
self.train_date = train_date
# 放在这里会触发垃圾回收机制,即当调用类后,被销毁,而driver在类里面,也会销毁所有放在全局里面
# self.driver = webdriver.Chrome()
def login(self):
driver.get(self.login_url)
WebDriverWait(driver,100).until(
EC.url_contains(self.personal_url) #包含个人信息网址
)
print('登录成功')
def run(self):
self.login()
def main():
spider = TrainSpider('北京', '上海', '2021-05-11')
spider.run()
if __name__ == '__main__':
main()
余票查询
获取站点信息
import requests
import re
import csv
url ='https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9028'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36'
}
req =requests.get(url=url,headers=headers)
req.encoding ='utf-8'
# print(req.text)
reqt =req.text
## 规律:
# @bjb|北京北|VAP|beijingbei|bjb|
# 0@bjd|北京东|BOP|beijingdong|bjd|
# 1@bji|北京|BJP|beijing|bj|
# 2@bjn|北京南|VNP|beijingnan|bjn|
station =re.findall(r'([\u4e00-\u9fa5]+)\|([A-Z]+)',reqt)
a = dict(station)
# print(a)
# print(station1)
# station =dict(station,indent=4)
# print((station))
lst =[]
for stations,index in enumerate(a):
dict ={}
# dict['name'] =
# print(index,a[index])
dict['name'] =index
dict['编码'] =a[index]
# print(dict)
lst.append(dict)
# filname =('name','编码')
# pd.DataFrame(station,index=[0]).to_csv('站点信息.csv',encoding='utf-8')
headers =('name','编码')
with open('12306车站编码.csv','w',encoding='utf-8',newline='')as f:
writer =csv.DictWriter(f,fieldnames=headers)
writer.writeheader()
writer.writerows(lst)
编码完成后,把他放在类中
结果:
添加目的地和出发地编码并点击出车次表
车次规律——把有datetrain的过滤
为了更好的把一个车次的信息展示成一个列表,从不同信息隔行发现有换行符\n,所以把\n换成空格’ ‘,再split分割’ ’
新增车次和座位
以长沙到北京的列车为例
点击预定的代码有点冗长(两段相同的,所以抽出来)
运行就进入提交订单页面
确认页面信息操作——找到、确认乘车人(一个或多个)并按勾确认
确认座位信息——要用到select下拉框语句
提交订单——while循环(当按钮第一次点不了)