三十、selenium实战– 解析列车信息(2)
续上例,示例代码
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import csv
# 为什么需要把driver放在外面?
# 因为如果放在里面,那么driver将会随着对象的销毁而被销毁
# 而我们的TrainSpider的对象是放在main函数中执行的。
# 只要main函数运行完成后,里面所有的变量都会被销毁,
# 也就是说spider也会被销毁,那么spider里面的driver也会被销毁。
driver = webdriver.Chrome(executable_path="E:\python\chromedriver\chromedriver.exe")
class TrainSpider(object):
login_url ="https://kyfw.12306.cn/otn/resources/login.html"
personal_url ="https://kyfw.12306.cn/otn/view/index.html"
left_ticket_url ="https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc"
def __init__(self, from_station,to_station, train_date, trains):
"""
:param from_station:起始站
:param to_station:目的站
:param train_date:出发日期
:param trains:需要购买的车次。("G7":["M","O"],"G3":["M","O"])
"""
self.from_station = from_station
self.to_station = to_station
self.train_date = train_date
self.trains = trains
# 初始化站点所对应的代号
self.station_codes = {}
self.init_station_code()
def init_station_code(self):
with open("stations.csv",'r', encoding='utf-8') as fp:
reader = csv.DictReader(fp)
for line in reader:
name = line["name"]
code = line['code']
#print("name:%s,code:%s" % (name, code))
self.station_codes[name] = code
def login(self):
driver.get(self.login_url)
# 等待url是否变成个人中心的url,来判断是否登录成功
WebDriverWait(driver, 1000).until(
EC.url_contains(self.personal_url)
)
print("登录成功!")
def search_left_ticket(self):
driver.get(self.left_ticket_url)
# 起始站的代号设置
from_station_input =driver.find_element_by_id("fromStation")
from_station_code =self.station_codes[self.from_station]
driver.execute_script("arguments[0].value='%s'" %from_station_code, from_station_input)
# 终点站的代号设置
to_station_input =driver.find_element_by_id("toStation")
to_station_code =self.station_codes[self.to_station]
driver.execute_script("arguments[0].value='%s'" %to_station_code, to_station_input)
# 时间设置
train_date_input =driver.find_element_by_id("train_date")
driver.execute_script("arguments[0].value='%s'"% self.train_date, train_date_input)
# 执行查询操作
search_btn = driver.find_element_by_id("query_ticket")
search_btn.click()
# 解析车次信息
WebDriverWait(driver, 1000).until(
EC.presence_of_element_located((By.XPATH,"//tbody[@id='queryLeftTable']/tr"))
)
train_trs = driver.find_elements_by_xpath("//tbody[@id='queryLeftTable']/tr[not(@datatran)]")
is_searched = False
for train_tr in train_trs:
infos = train_tr.text.replace("\n"," ").split(" ")
number = infos[0]
if number in self.trains:
seat_types =self.trains[number]
for seat_type in seat_types:
# 二等座
if seat_type =="O":
count = infos[9]
if count.isdigit() orcount == '有':
is_searched = True
break
elif seat_type == 'M':
# 一等座
count = infos[8]
if count.isdigit() orcount == '有':
is_searched = True
break
if is_searched:
order_btn =train_tr.find_element_by_xpath(".//a[@class='btn72']")
order_btn.click()
break
def run(self):
# 1、登录
self.login()
# 2、车次余票查询
self.search_left_ticket()
def main():
spider = TrainSpider("北京", "上海","2020-02-20", {"G7": ["M", "O"]})
spider.run()
if__name__ == '__main__':
main()
上一篇文章 第五章 爬虫进阶(二十九) 2020-02-15 地址:
https://www.jianshu.com/p/922236495c94
下一篇文章 第五章 爬虫进阶(三十一) 2020-02-17 地址:
https://www.jianshu.com/p/96e3c01ad21f
以上资料内容来源网络,仅供学习交流,侵删请私信我,谢谢。